When to use DataContract and DataMember attributes?

I am very confused about the DataContract attribute in WCF. As per my knowledge it is used for serializating user defined type like classes. I wrote one class which is exposed at client side like this.

[DataContract]
public class Contact
{
    [DataMember]
    public int Roll { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Address { get; set; }

    [DataMember]
    public int Age { get; set; }
}

It is working properly but when I remove DataContract and DataMember it also works properly. I can’t understand why it is working properly. Can any one tell me what is the actual use of DataContract?

My service contract looks like this

[ServiceContract]    
public interface IRestServiceImpl
{
    [OperationContract]        
    Contact XmlData(string id);      
}

8 Answers
8

Leave a Comment