次の方法で共有


逐語的テキスト - 変数、属性、および文字列リテラルの@

@特殊文字は、逐語的な識別子として機能します。 次の方法で使用します。

  1. 文字列リテラルを逐語的に解釈することを示す場合。 このインスタンスの @ 文字は、 逐語的な文字列リテラルを定義します。 単純なエスケープ シーケンス (バックスラッシュの場合は "\\" など)、16 進エスケープ シーケンス (大文字 A の場合は "\x0041" など)、Unicode エスケープ シーケンス (大文字 A の場合は "\u0041" など) は文字どおり解釈されます。 引用符エスケープ シーケンス ("") のみがリテラルで解釈されず、1 つの二重引用符が生成されます。 さらに、逐語的に 補間された文字列 中かっこエスケープ シーケンス ({{ および }}) の場合、リテラルでは解釈されません。単一の中かっこ文字が生成されます。 次の例では、2 つの同じファイル パスを定義します。1 つは通常の文字列リテラルを使用し、もう 1 つは逐語的な文字列リテラルを使用して定義します。 これは、逐語的な文字列リテラルのより一般的な用途の 1 つです。

    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
    

    次の例は、通常の文字列リテラルと、同じ文字シーケンスを含む逐語的な文字列リテラルを定義する効果を示しています。

    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"
    
  2. 識別子として C# キーワードを使用する。 @文字は、コンパイラが C# キーワードではなく識別子として解釈するコード要素にプレフィックスを付けます。 次の例では、@文字を使用して、for ループで使用するforという名前の識別子を定義します。

    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!
    
  3. 名前付けの競合が発生した場合にコンパイラで属性を区別できるようにするため。 属性は、 Attributeから派生するクラスです。 通常、その型名にはサフィックス Attribute が含まれますが、コンパイラではこの規則は適用されません。 この属性は、完全な型名 ( [InfoAttribute] や短縮名 ( [Info] など) によってコード内で参照できます。 ただし、2 つの短縮属性の型名が同じで、一方の型名に 属性 サフィックスが含まれているが、もう一方の型名には含まれていない場合、名前付けの競合が発生します。 たとえば、次のコードは、 Info 属性または InfoAttribute 属性が Example クラスに適用されているかどうかをコンパイラが判断できないため、コンパイルに失敗します。 詳細については、 CS1614 を参照してください。

    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' ([@Info("A simple executable.")]). Specify the full name 'InfoAttribute' to select it.
    public class Example
    {
       [InfoAttribute("The entry point.")]
       public static void Main()
       {
       }
    }
    

こちらも参照ください