When defining a WCF data contract, which type should one use for collections/lists?
note: I'm answering this from the client's perspective - i.e. the /collectionType:<type>
switch on svcutil.exe
(also available in the IDE).
Personally, I tend to keep it simple and use List<T>
. If you are going to do lots of data binding, BindingList<T>
might be an option, but for object properties it is usually overkill. Arrays make life very hard... avoid them ;-p
Note that with .NET 3.5 the features available to each collection type blur, thanks to the extension methods on Enumerable
.
Normally, Collection<T>
is useful when you think you might want to subclass the collection to use the virtual
extension points. This isn't really an option with WCF.
As already stated, using IList<T>
etc isn't an option unless you are using assembly sharing, since the generated class won't be able to create the collection.
You can not use interface type in datacontract because the serializer won't work with interface type properties.
You can use concrete type e.g. MyClass[] or List
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With