The most common use of the ‘@’ is escape character for strings that contain backslashes in them [\]. So as things like code below is possible.
1: string s = @"c:\windows\system32\";
2: string s = @"this will retain my \r\n and not change to a line break";
But another usage is as an prefix for C# keywords. In the code below you will notice that the keywords class static and bool are prefixed with the @ prefix.
1: public class @class
2: {
3: public static void @static(bool @bool)
4: {
5: if (@bool)
6: System.Console.WriteLine("true");
7: else
8: System.Console.WriteLine("false");
9: }
10: }
11: public class Class1
12: {
13: [STAThread]
14: public static void Main()
15: {
16: cl\u0061ss.st\u0061tic(true);
17: @class.@static(false);
18: //class.static(true); //will not work
19: }
20: }
What this also enables is a scenario like the one below, which I do not like.
1: public class @class
2: {
3: public static void MyCoolMethod(bool @theBoolValue)
4: {
5: if (theBoolValue)
6: System.Console.WriteLine("true");
7: else
8: System.Console.WriteLine("false");
9: }
10: }
11: public class Class1
12: {
13: [STAThread]
14: public static void Main()
15: {
16: cl\u0061ss.MyCoolMethod(true);
17: @class.MyCoolMethod(false);
18: }
19: }
Spot the difference? In the method signature of MyCoolMethod the boolean parameter has the @ prefix however in the code below where the value of the boolean is being interrogated I have skipped using the identifier. This code will however work exactly the same. The reason for is simple
The identifiers are the same if ‘@’ prefix is removed when used, therefore @theBooleanValue and theBooleanValue are same
To quote MSDN on identifiers
Two identifiers are considered the same if they are identical after the following transformations are applied, in order:
- The prefix "@", if used, is removed.
- Each unicode-escape-sequence is transformed into its corresponding Unicode character.
- Any formatting-characters are removed
Most importantly the general advice on using the @ prefix on words that are not keywords is NOT to use it. And having seen it in used in code have an instant distaste for it. I wonder if FxCop has a rule for checking the usage of the @ prefix for non keywords in identifiers?







