Data contract is an agreement between client and Service about the data, which is to be exchanged between them. The important thing to remember is that both of them need to share data contract only rather than the data types for the communication. WCF (Windows Communication Foundation) uses serialization engine to serialize and deserialize the data. [DataContract] attribute specifies the data, which is to serialize (in short conversion of structured data into some format like Binary, XML etc.) and deserialize(opposite of serialization) in order to exchange between the client and the Service. Data Contract attribute can be annotated on a class, Enum or even a structure but not on an interface.
Example
- [DataContract]
- public class Student
- {
- [DataMember]
- public int ID;
- [DataMember]
- public string Name;
- }
[DataContract] attribute parameters
IsReference
This parameter is of bool type and specifies that whether to preserve the object reference data or not. Its default value is false. To keep object reference data preserve, set its value to true.
Example
- [DataContract(IsReference =true)]
- public class Student
- {
- [DataMember]
- public int ID;
- [DataMember]
- public string Name;
- }
Name
Example
- [DataContract(Name ="StudentInformation")]
- public class Student
- {
- [DataMember]
- public int ID;
- [DataMember]
- public string Name;
- }
Namespace
This parameter takes a URL template and is used to specify the namespace for Data Contract. The default value of this parameter comes from CLR namespace.
Example
- [DataContract(Namespace = "http://www.StudentInformationManagement.com")]
- public class Student
- {
- [DataMember]
- public int ID;
- [DataMember]
- public string Name;
- }

Join the conversation! Your thoughts help the community grow.