Use of the @ flag for C#This blog was written in MarkDown

Posted by EricS on Sat, 27 Jul 2019 03:39:15 +0200

@ (C#Reference--from official documents Entrance)

1. Make the C#keyword an identifier.The @character can be used as a prefix to a code element that the compiler will interpret as an identifier rather than a C#keyword.The following example uses the @ character to define an identifier named for used in the 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. Indicates that the string will be interpreted literally.The @character defines a literal identifier in this instance.Simple escape sequences (such as'\', which represents backslashes), hexadecimal escape sequences (such as'\x0041', which represents uppercase A), and Unicode escape sequences (such as'\0041', which represents uppercase A) will all be interpreted literally.Only the quotation escape sequence ('') is not literally interpreted; it generates single quotation marks.In addition, if you interpolate strings word by word, the brace escape sequences ({{and}}) are not literally interpreted; they generate a single brace character.The following example defines two identical file paths using a regular string and a primitive string, respectively.This is one of the more common uses of literal strings.

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 demonstrates the effect of defining regular and primitive strings that contain the same sequence of characters.

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. Make the compiler distinguish between two attributes in case of naming conflicts.Attributes are classes derived from Attributes.Its type name usually contains the suffix Attribute, but the compiler will not force this conversion.This property can then be referenced in code by its full type name (for example, [InfoAttribute]) or short name (for example, [Info]).However, if two short names are the same, and one type name contains an Attribute suffix and the other does not, a naming conflict occurs.For example, the code below cannot be compiled because the compiler cannot determine whether to apply Info or InfoAttribute attributes to the Example class.

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()
   {
   }
}

Topics: PHP Attribute