Reputation: 4774
Why are fields not serializable via the XML serializer, in C#, whereas properties are? I know that properties are actually methods, but I don't see why that should make a difference. Does anyone know the answer to this?
Thanks so much!
Upvotes: 0
Views: 199
Reputation: 6876
Fields are serializable. In fact, all fields are by default serialized unless you mark them with the NonSerialized
attribute.
If you talk about XML serialization, then only public fields will be serialized because XML serialization only serializes the public interface of an object.
Upvotes: 2
Reputation: 2466
Fields are serializable :
XML serialization serializes the public fields and properties of an object, or the parameters and return values of methods, into an XML stream that conforms to a specific XML Schema definition language (XSD) document. XML serialization results in strongly typed classes with public properties and fields that are converted to XML. System.Xml.Serialization contains the classes necessary for serializing and deserializing XML.
From MSDN
Upvotes: 7