Monthly Archives: June 2014

EmitDefaultValue Significance

In .Net, types have default value. Like for reference type default value is null, and for integer type default value is 0.
It is desirable to omit data member from serialized data which is set to it’s default value as a performance advantage.

To omit a member from serialised data, set the EmitDefaultValue property of the attribute to false. Default is true.

[DataContract]
public class Employee
{
    [DataMember]
    public string employeeName = null;

    [DataMember]
    public int employeeID = 0;

    [DataMember(EmitDefaultValue = false)]
    public string position = null;

    [DataMember(EmitDefaultValue = false)]
    public int salary = 0;

    [DataMember(EmitDefaultValue = false)]
    public int? bonus = null;

    [DataMember(EmitDefaultValue = false)]
    public int targetSalary = 57800;
}

XML Representation

<Employee>
   <employeeName xsi:nil="true" />
   <employeeID>0</employeeID>
<targetSalary>57800</targetSalary>
</Employee>

Refrences:
Data Member Default Values

Leave a comment

Filed under WCF