Is the DataMember attribute set to a field or property?

In which case should I use the DataMemeber attribute?

I.

[DataMember] internal protected string _FirstName=""; [DataMember] public string FirstName { get { return _FirstName; } internal protected set { _FirstName=(value!=null?value:""); } } 

II.

 internal protected string _FirstName=""; [DataMember] public string FirstName { get { return _FirstName; } internal protected set { _FirstName=(value!=null?value:""); } } 

III.

 [DataMember] internal protected string _FirstName=""; public string FirstName { get { return _FirstName; } internal protected set { _FirstName=(value!=null?value:""); } } 
+6
source share
2 answers

The first one is definitely wrong, as serialization will happen twice. Between the 2nd and 3rd, I personally prefer the second, as an encapsulating implementation.

+7
source

Second. This provides only the property as a data item. This is what you want. You do not want the field to be open.

+4
source

Source: https://habr.com/ru/post/895882/


All Articles