Windows Phone Developers

Tuesday, December 16, 2008

How to use Reserved Words in .NET (C#)

How to use Keywords in .NET (C#)

If you can use tens and thousands of words as identifier, why should one use the few keywords reserved by C#?

private static void KeywordExample()

{

string string = "Some String";

}

Might be you still want to use ‘bool’, ‘string’ etc. But using this will throw “identifier expected, 'keyword' is a keyword - Compiler Error CS1041”

A reserved word for the C# language was found where an identifier was expected. Replace the keyword with a user-specified identifier.

To overcome this error, prefix the identifier with “@”. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier.

private static void KeywordExample()

{

string @string = "Some String";

Console.Write(@string);

}

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

4 comments:

  1. I would fire IMMEDIATELY anyone who abused their fellow developers by doing that. Horrid, HORRID idea.

    ReplyDelete
  2. Easy, IDisposable! What about this use case?

    ReplyDelete
  3. although normally unusual, this solution is great for parsing xml with elements or attributes using reserved words. In example: you have a restriction defined in a xsd file saying "true" and "false". Now, how would you generate a class to serialize/deserialize that without having the @ option?

    www.hjgode.de/wp

    ReplyDelete
  4. I would fire you! There are legitimate reasons, one of which would be using keywords in an enum and testing against the enum for membership with text.

    ReplyDelete