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

JacobE picture JacobE · Feb 27, 2009 · Viewed 7.3k times · Source

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?

Answer

Marc Gravell picture Marc Gravell · Feb 27, 2009

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.