Undocumented .Net The Out Campaign: Scarlet Letter of Atheism

Thursday, December 20, 2007

Use reserved keywords as member names

Did you know that you can use reserved keywords as member names like this: "public string string;". To do that all you have to do is use an "@" sign on the start of the field or property name as demonstrated on the left. When you access this member in code it shows with the "@" sign.

Well you would use that with xml serialization and deserialization where the element name is equal to a reserved keywords in the specific language you are using. It is not a good idea to use reserved keywords as member names, however you are not in way because of the "@" sign you have to prefix the member with. When deserializing xml that has elements named the same as reserved keywords, the xml serializer will match "" to "public string @string;" with out making use of any attribute decoration i.e. XmlElementAttribute with the ElementName property set to "string", like this "[XmlElement(ElementName="string"]public string StringField;".

I hope this helps.

kick it on DotNetKicks.com

0 comments:

Post a Comment

Daniel Portella - http://undocnet.blogspot.com - http://twitter.co/dmportella - This posting is provided "AS IS" with no warranties, and confers no rights.