@(C# 参考)@ (C# Reference)

@ 特殊字符用作原义标识符。The @ special character serves as a verbatim identifier. 它具有以下用途:It can be used in the following ways:

  1. 使 C# 关键字用作标识符。To enable C# keywords to be used as identifiers. @ 字符可作为代码元素的前缀,编译器将把此代码元素解释为标识符而非 C# 关键字。The @ character prefixes a code element that the compiler is to interpret as an identifier rather than a C# keyword. 下面的示例使用 @ 字符定义其在 for 循环中使用的名为 for 的标识符。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. 简单转义序列(如代表反斜杠的 "\\")、十六进制转义序列(如代表大写字母 A 的 "\x0041")和 Unicode 转义序列(如代表大写字母 A 的 "\u0041")都将按字面解释。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 one double quotation mark. 此外,如果是逐字内插字符串,大括号转义序列({{}})不按字面解释;它们会生成单个大括号字符。Additionally, in case of a verbatim interpolated string brace escape sequences ({{ and }}) are not interpreted literally; they produce single brace characters. 下面的示例分别使用常规字符串和原义字符串定义两个相同的文件路径。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 example displays the following output:
    //     c:\documents\files\u0066.txt
    //     c:\documents\files\u0066.txt
    

    下面的示例演示定义包含相同字符序列的常规字符串和原义字符串的效果。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. 属性是派生自 Attribute 的类。An attribute is a class that derives from Attribute. 其类型名称通常包含后缀 Attribute,但编译器不会强制进行此转换。Its type name typically includes the suffix Attribute, although the compiler does not enforce this convention. 随后可在代码中按其完整类型名称(例如 [InfoAttribute])或短名称(例如 [Info])引用此属性。The attribute can then be referenced in code either by its full type name (for example, [InfoAttribute] or its shortened name (for example, [Info]). 但是,如果两个短名称相同,并且一个类型名称包含 Attribute 后缀而另一类型名称不包含,则会出现命名冲突。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. 例如,由于编译器无法确定将 Info 还是 InfoAttribute 属性应用于 Example 类,因此下面的代码无法编译。For example, the following code fails to compile because the compiler cannot determine whether the Info or InfoAttribute attribute is applied to the Example class. 有关详细信息,请参阅 CS1614See CS1614 for more information.

    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.")] // Generates compiler error CS1614. Ambiguous Info and InfoAttribute.
    // Prepend '@' to select 'Info'. Specify the full name 'InfoAttribute' to select it.
    public class Example
    {
       [InfoAttribute("The entry point.")]
       public static void Main()
       {
       }
    }
    

请参阅See also