Data Member Attribute And Its Parameters in WCF

Data Member attribute

Data Member are the fields or properties of your Data Contract class. You must specify [DataMember] attribute on the property or the field of your Data Contract class to identify it as a Data Member. DataContractSerializer will serialize only those members, which are annotated by [DataMemeber] attribute. You can define Data Members by just placing [DataMemeber] attribute on the property or the field.

Example
  1. [DataContract(IsReference =true,Name ="StudentInformation", Namespace = "http://www.StudentInformationManagement.com")]  
  2.     public class Student  
  3.     {  
  4.         [DataMember]  
  5.         public int ID=0;  
  6.         [DataMember]  
  7.         public string Name=null;  
  8.         [DataMember]  
  9.         public int Fees = 500;  
  10.     }   
[DataMember] attribute parameters

[DataMember] has some parameters, which specify the details about Data Member. The name of these parameters and their functions are given below.

EmitDefaultValue

This parameter specifies that whether or not, you are required to serialize the default value of a data member. Its default value is true.

Example

  1. [DataContract(IsReference =true,Name ="StudentInformation", Namespace = "http://www.StudentInformationManagement.com")]  
  2.     public class Student 
  3.     {  
  4.         [DataMember]  
  5.         public int ID=0;  
  6.         [DataMember]  
  7.         public string Name=null;  
  8.         [DataMember(EmitDefaultValue =false)]  
  9.         public int Fees = 500;  
  10.     }  

In the example given above, the default value of ID and Name will be serialized as their EmitDefaultValue is true , while the value of Fees will not be serialized as its EmitDefaultValue is false

IsRequired

This parameter is used to specify whether the data member is required or not. The true value of this parameter informs serialization engine that this data member must be present.

Example   

  1. [DataContract(IsReference = true, Name = "StudentInformation", Namespace = "http://www.StudentInformationManagement.com")]  
  2. public class Student {  
  3.     [DataMember(IsRequired = true)]  
  4.     public int ID = 0;  
  5.     [DataMember]  
  6.     public string Name = null;  
  7.     [DataMember(EmitDefaultValue = false, IsRequired = false)]  
  8.     public int Fees = 500;  
  9. }  
In the example given above, IsRequired true parametervalue of ID Data Member tells the serialization engine that it is required and Fees is not required.

Name

This parameter is used to get or set the name of the data member. The default value of this parameter is the name of the property or the field on which it is applied.

Example
  1. [DataContract(IsReference = true, Name = "StudentInformation", Namespace = "http://www.StudentInformationManagement.com")]  
  2. public class Student {  
  3.     [DataMember(IsRequired = true)]  
  4.     public int ID = 0;  
  5.     [DataMember(Name = "StudentName")]  
  6.     public string Name = null;  
  7.     [DataMember(EmitDefaultValue = false, IsRequired = false)]  
  8.     public int Fees = 500;  
  9. }  

Order 

The order parameter determines the order in which the data is sent or received. It also specifies the serialization and deserialization order of a data member

Example
  1.    [DataContract(IsReference =true,Name ="StudentInformation", Namespace = "http://www.StudentInformationManagement.com")]  
  2.     public class Student  
  3.     {  
  4.         [DataMember(IsRequired =true,Order =0)]  
  5.         public int ID=0;  
  6.         [DataMember(Name ="StudentName",Order =1)]  
  7.         public string Name=null;  
  8.         [DataMember(EmitDefaultValue =false,IsRequired =false,Order =2)]  
  9.         public int Fees = 500;  
  10.     }   
Data Member attribute is very useful for defining the rules or policies for Data Members of your Data Contract.
Ebook Download
View all
Learn
View all