Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which list/collection type is best to use in a WCF data contract?

When defining a WCF data contract, which type should one use for collections/lists?

  • Should it be ICollection<T>, IList<T>, T[] or...?
  • Should I use interface types or the concrete types?
  • What trade offs are there to consider?
like image 635
JacobE Avatar asked Feb 27 '09 12:02

JacobE


2 Answers

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.

like image 87
Marc Gravell Avatar answered Nov 04 '22 01:11

Marc Gravell


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

like image 43
Ray Lu Avatar answered Nov 04 '22 00:11

Ray Lu