WCF CollectionDataContract

stackoverflowuser picture stackoverflowuser · Apr 21, 2011 · Viewed 12.1k times · Source

I recently noticed in one of the articles the wcf service operation returned a collectiondatacontract

Users GetUsers(string someInput);

And Users type was defined as below:

[CollectionDataContract]
    public class Users : List<User>
    {
        public Users()
        {
        }

        public Users(IEnumerable<User> users) : base(users)
        {
        }
    }

Does returning a collectiondatacontract (like Users in this case) serve a different purpose than simply returning List<User> ?

Answer

Andrei picture Andrei · Apr 21, 2011

As far as I understand, this attribute will give you some control over what names the elements will have in the final xml string, after the DataContractSerializer has done its work serializing your collection.

This can be useful when you have to parse the result later manualy( in other words you will know what element to look for in that xml text, to find your collection and its parts).

Take a look at this for examples and more info:

http://msdn.microsoft.com/en-us/library/aa347850.aspx