The function of @ sign in C

Time:2020-2-17

Today, we have a problem: how to put the complex SQL with single quotation mark and double quotation mark into string. Farmat (@ “”)? I encountered a keyword @. I used @ to make the SQL statement wrap, but I encountered the problem of double quotation marks.

I suddenly forgot how to escape the double quotation marks. Using the backslash is not easy to use. Because there is @ keyword, it needs to use a pair of double quotation marks to escape the @ keyword. Baidu in the background inquired the articles of other Taoists and recorded them for sharing:

 

A key:Add @ before the string, and the escape string in the string will no longer be escaped. Exception: ‘will still be escaped to’, {{and}} will still be escaped to {and}. When using both string interpolation and literal strings, $should precede @

 

Daoyou article: https://www.cnblogs.com/zhaochenxi/p/10570482.html

 

Refer to Microsoft official documents – special characters @, address: https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/tokens/verbatim

1. Add @ before the variable name to tell the compiler that @ is followed by the variable name. It is mainly used when the variable name and C ා keyword are duplicate.

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. Add @ before the string, and the escape string in the string will no longer be escaped. Exception: ‘will still be escaped to’, {{and}} will still be escaped to {and}. When using both string interpolation and literal strings, $should precede @

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

3. Similar to the first article, it is used to distinguish two property names when naming conflicts. Attribute attribute custom type names should end with attribute when they are named, such as infoattribute, which we can then reference with infoattribute or info. But if we define two custom attributes, named info and infoattribute respectively, the compiler will not know which is when using the name info. At this time, if you want to use info, use @ info. If you want to use infoattribute, write all the names.

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

Recommended Today

The application of USB camera in rk3399

The application of USB camera in rk3399 1, introduction UVCFull nameUSB Video Class, is a set of standard customized by usb-if. All USB interface cameras complying with this standard can almost be used directly under Windows Linux and other systems, achieving the similar effect of drive free. Of course, it doesn’t mean that there is […]