@ (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. 簡單逸出序列 (例如 "\\" 用於反斜線)、十六進位逸出序列 (例如 "\x0041" 用於大寫 A),以及 Unicode 逸出序列 (例如 "\u0041" 用於大寫 A) 都是照字面解譯。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. 此外,如果是逐字插入字串,大括弧逸出序列 ({{}}) 不會照字面意義解譯,它們會產生一個大括弧字元。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. 例如,下列程式碼無法編譯,因為編譯器無法判斷是否已對 Example 類別套用了 InfoInfoAttribute 屬性。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. 請參閱 CS1614 以取得詳細資訊。See 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