IgnoreNullMembers Property
Namespace
System.Runtime.Serialization
Assembly
System.Runtime.Serialization.Formatters.dll
Overview
Gets or sets a value that indicates whether members with null values should be ignored during serialization.
Syntax
public bool IgnoreNullMembers { get; set; }
Property Value
- true – Members with
nullvalues are omitted from the serialized data. - false – All members are serialized regardless of their value.
Remarks
Show/Hide Details
Setting IgnoreNullMembers to true can reduce the size of the serialized payload, especially when many reference-type members are null. However, deserialization may require additional handling if the target type expects those members to exist.
This property is commonly used with DataContractSerializer, BinaryFormatter, and custom serializers that respect the IgnoreNullMembers flag.
Examples
// Example: Using IgnoreNullMembers with DataContractSerializer
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
[DataContract]
public class Person
{
[DataMember] public string Name { get; set; }
[DataMember] public string Email { get; set; }
[DataMember] public string Phone { get; set; }
}
public class Demo
{
public static void Main()
{
var p = new Person { Name = "Alice", Email = null, Phone = "555‑1234" };
var settings = new DataContractSerializerSettings
{
// Assuming the serializer respects IgnoreNullMembers via options
// (Pseudo-code, adjust for actual API)
// Options = new SerializationOptions { IgnoreNullMembers = true }
};
var serializer = new DataContractSerializer(typeof(Person), settings);
using var ms = new MemoryStream();
using var writer = XmlWriter.Create(ms);
serializer.WriteObject(writer, p);
writer.Flush();
ms.Position = 0;
Console.WriteLine(new StreamReader(ms).ReadToEnd());
}
}