Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0006 Adding @ in front of a keyword

If we have to use a keyword as an identifier we can add @ to the keyword as prefix. The following code use the public as an identifer. using System; class @public { static void Main(string[] args) { int i = 0; int j = 2; Console.WriteLine("i=" + i); Console.WriteLine("j=" + j); } } The output: i=0 j=2 If we use public without @ as the class name, C# compiler generates the following error message. C:\g>csc Program.cs Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1 Copyright (C) Microsoft Corporation. All rights reserved. Program.cs(3,7): error CS1041: Identifier expected; 'public' is a keyword Program.cs(3,7): error CS1513: } expected Program.cs(4,1): error CS1518: Expected class, delegate, enum, interface, or struct Program.cs(5,12): error CS1518: Expected class, delegate, enum, interface, or struct Program.cs(5,29): error CS1001: Identifier expected Program.cs(5,31): error CS1518: Expected class, delegate, enum, interface, or struct Program.cs(13,1): error CS1022: Type or namespace definition, or end-of-file expected Please note that the @ is not part of the keyword itself.