Indicates that a field of a serializable class should not be serialized. This class cannot be inherited.
See Also: NonSerializedAttribute Members
When using the System.Runtime.Serialization.Formatters.Binary.BinaryFormatter or System.Runtime.Serialization.Formatters.Soap.SoapFormatter classes to serialize an object, use the NonSerializedAttribute attribute to prevent a field from being serialized. For example, you can use this attribute to prevent the serialization of sensitive data.
The target objects for the NonSerializedAttribute attribute are public and private fields of a serializable class. By default, classes are not serializable unless they are marked with SerializableAttribute. During the serialization process all the public and private fields of a class are serialized by default. Fields marked with NonSerializedAttribute are excluded during serialization. If you are using the System.Xml.Serialization.XmlSerializer class to serialize an object, use the System.Xml.Serialization.XmlIgnoreAttribute class to get the same functionality. Alternatively, implement the System.Runtime.Serialization.ISerializable interface to explicitly control the serialization process. Note that classes that implement System.Runtime.Serialization.ISerializable must still be marked with SerializableAttribute.
To apply the NonSerializedAttribute class to an event, set the attribute location to field, as shown in the following C# code.
Example
[field:NonSerializedAttribute()] public event ChangedEventHandler Changed;
If a field is not serialized, but it still requires a default value that must be supplied after deserialization, you can create a method that supplies the field with a value, then apply the System.Runtime.Serialization.OnDeserializedAttribute to the method.
For more information about using attributes, see Extending Metadata Using Attributes.