축자 텍스트 - 변수, 특성 및 문자열 리터럴의 @

@ 특수 문자는 축자 식별자로 사용됩니다. 다음과 같은 방법으로 사용합니다.

  1. 문자열 리터럴이 축자로 해석될 것임을 나타냅니다. 이 인스턴스의 @ 문자는 축자 문자 문자열을 정의합니다. 단순 이스케이프 시퀀스(예: 백슬래시에 "\\"), 16진수 이스케이프 시퀀스(예: 대문자 A에 "\x0041"), 유니코드 이스케이프 시퀀스(예: 대문자 A에 "\u0041")는 문자 그대로 해석됩니다. 인용 부호 이스케이프 시퀀스("")만 문자 그대로 해석되지 않고 하나의 큰따옴표를 생성합니다. 또한 축자 보간된 문자열 중괄호 이스케이프 시퀀스({{}})는 그대로 해석되지 않습니다. 단일 중괄호 문자를 생성합니다. 다음 예제에서는 각각 일반 문자열 리터럴 및 축자 문자열 리터럴을 사용하여 두 개의 동일한 파일 경로를 정의합니다. 이것은 축자 문자열 리터럴의 일반적인 사용법 중 하나입니다.

    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])으로 코드에서 특성을 참조할 수 있습니다. 그러나 두 개의 약식 특성 유형 이름이 동일하고 한 유형 이름에만 Attribute 접미사가 포함된 경우 이름 충돌이 발생합니다. 다음 예에서는 컴파일러가 InfoInfoAttribute 특성 중 무엇을 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()
       {
       }
    }
    

참고 항목