다음을 통해 공유


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

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

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

    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파생되는 클래스입니다. 컴파일러가 이 규칙을 적용하지는 않지만 해당 형식 이름에는 일반적으로 접미사 특성이 포함됩니다. 그런 다음 전체 형식 이름(예 [InfoAttribute] : 단축된 이름) [Info]으로 코드에서 특성을 참조할 수 있습니다. 그러나 두 개의 단축된 특성 형식 이름이 동일하고 한 형식 이름에 특성 접미사가 포함되지만 다른 이름은 그렇지 않은 경우 명명 충돌이 발생합니다. 예를 들어 컴파일러에서 클래스에 InfoAttribute 특성이 적용되는지 여부를 Info 확인할 수 없으므로 다음 코드는 컴파일에 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()
       {
       }
    }
    

참고하십시오