Thursday, January 7, 2010

How to use Reserved Words 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);
}