@ (C# Reference)

The @ special character serves as a verbatim identifier. It can be used in the following ways:

  1. To enable C# keywords to be used as identifiers. The @ character prefixes a code element that the compiler is to interpret as an identifier rather than a C# keyword. The following example uses the @ character to define an identifier named for that it uses in a for loop.

    string[] @for = { "John", "James", "Joan", "Jamie" };
    for (int ctr = 0; ctr < @for.Length; ctr++)
    {
       Console.WriteLine($"Here is your gift, {@for[ctr]}!");
    }
    // The example displays the following output:
    //     Here is your gift, John!
    //     Here is your gift, James!
    //     Here is your gift, Joan!
    //     Here is your gift, Jamie!
    
  2. To indicate that a string literal is to be interpreted verbatim. The @ character in this instance defines a verbatim string literal. Simple escape sequences (such as "\\" for a backslash), hexadecimal escape sequences (such as "\x0041" for an uppercase A, and Unicode escape sequences, such as "\u0041" for an uppercase A, are interpreted literally. Only a quote escape sequence ("") is not interpreted literally; it produces a single quotation mark. The following example defines two identical file paths, one by using a regular string literal and the other by using a verbatim string literal. This is one of the more common uses of verbatim string literals.

    string filename1 = @"c:\documents\files\u0066.txt";
    string filename2 = "c:\\documents\\files\\u0066.txt";
    
    Console.WriteLine(filename1);
    Console.WriteLine(filename2);
    

    The following example illustrates the effect of defining a regular string literal and a verbatim string literal that contain identical character sequences.

    string s1 = "He said, \"This is the last \u0063hance\x0021\"";
    string s2 = @"He said, ""This is the last \u0063hance\x0021""";
    
    Console.WriteLine(s1);
    Console.WriteLine(s2);
    // The example displays the following output:
    //     He said, "This is the last chance!"
    //     He said, "This is the last \u0063hance\x0021"      
    
  3. To enable the compiler to distinguish between attributes in cases of a naming conflict. An attribute is a type that derives from Attribute. Its type name typically includes the suffix Attribute, although the compiler does not enforce this convention. The attribute can then be referenced in code either by its full type name (for example, [InfoAttribute] or its shortened name (for example, [Info]). However, a naming conflict occurs if two shortened attribute type names are identical, and one type name includes the Attribute suffix but the other does not. For example, the following code fails to compile because the compiler cannot determine whether the Info or InfoAttribute attribute is applied to the Main method.

    using System;
    
    [AttributeUsage(AttributeTargets.Class)]
    public class Info : Attribute
    {
       private string information;
    
       public Info(string info)
       {
           information = info;
       }
    }
    
    [AttributeUsage(AttributeTargets.Method)]
    public class InfoAttribute : Attribute
    {
       private string information;
    
       public InfoAttribute(string info)
       {
           information = info;
       }
    }
    
    [Info("A simple executable.")]
    public class Example
    {
       [InfoAttribute("The entry point.")]
       public static void Main()
       {
       }
    }
    

    If the verbatim identifier is used to identify the Info attribute, the example compiles successfully.

    using System;
    
    [AttributeUsage(AttributeTargets.Class)]
    public class Info : Attribute
    {
       private string information;
       
       public Info(string info)
       {
          information = info;
       }
    }
    
    [AttributeUsage(AttributeTargets.Method)]
    public class InfoAttribute : Attribute
    {
       private string information;
       
       public InfoAttribute(string info)
       {
          information = info;
       }
    }
    
    [@Info("A simple executable.")]
    public class Example
    {
       [InfoAttribute("The entry point.")]
       public static void Main()
       {
       }
    }
    

See Also

C# Reference
C# Programming Guide
C# Special Characters