0

It might sound like a very dumb question but I didn't find a suitable answer after googling.

When you can access a Datamember directly using the property name what is the purpose of explicitly setting a data member name as shown below in bracket next to datamember attribute.

[DataMember(Name = "Status")]
public string Status { get; set; }
4
  • 4
    Have you read the doc? It's pretty explicit: When applied to the member of a type, specifies that the member is part of a data contract and is serializable by the DataContractSerializer
    – ken2k
    Aug 26, 2014 at 16:00
  • Most relevant part: By default, the CLR member name is used as the name of the data member. By setting the Name property, you can customize the name of the data member
    – ken2k
    Aug 26, 2014 at 16:01
  • Yes thats what I am seeking why would a customization of the name be helpful. Let say if Status property is self explanatory why would i add Name to datamember. Just adding the Datamember to the Status property will make it serializable
    – hellowahab
    Aug 26, 2014 at 16:05
  • I am more concerned about those usage scenarios which can't be achieved without these customization of name.
    – hellowahab
    Aug 26, 2014 at 16:05

2 Answers 2

3

The docs are pretty good actually:

The Name property enables you to use names that are not permitted as common language runtime (CLR) identifiers. In addition, this property enables the type author to define a data member name separate from the CLR member name. This separate definition helps in versioning scenarios (changing the CLR member name without breaking the data contract) and allows a different naming convention for data contact members and CLR members.

2

Datamember.Name just defines the representation of this property in the serialized form.

It is pretty handy if you serialize to JSON for example because in JavaScript you usually want to work with camelCased variables and in C# your models should have PascalCased names...

Also, read the remarks in the official docs, might help you to understand that, too.

1
  • See the competing answer. Aug 26, 2014 at 16:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.