Analyze several ways of using the @ symbol in C

  • 2020-05-10 18:46:31
  • OfStack

1. Qualify strings
The @ sign precedes a string to indicate that the escape character "no" is being processed.
If we write the path of 1 file, such as text.txt file under the path of "D:/ text file ", we write it as follows without the @ symbol:
stringfileName="D:// text file // text.txt ";
It's easier if you use the @ sign:
stringfileName=@"D:/ text file/text.txt ";

2. Make strings cross lines
Sometimes a string is too long to be written on a line 1 (SQL, for example) without the @ sign. Here's how to write it:

string strSQL="SELECT * FROM HumanResources.Employee AS e"   
+"INNER JOINPerson.Contact AS c"   3.+"ON e.ContactID=c.ContactID"   4.+"ORDERBY c.LastName";   

After adding the @ sign, you can directly wrap the line:

string strSQL=@"SELECT * FROM HumanResources.Employee AS e INNER JOIN Person.Contact AS c ON e.ContactID=c.ContactID ORDERBYc.LastName";   

3. Usage in identifiers
C# does not allow keywords to be used as identifiers (class names, variable names, method names, table space names, and so on), but it does if you add @, for example:

namespace @namespace   
{   
  class @class   4.     {   
        public static void @static(int @int)   6.         {   
            if (@int > 0)   8.             {   
                 System.Console.WriteLine("Positive Integer");   10.             }   
            else if (@int == 0)   12.             {   
                 System.Console.WriteLine("Zero");   14.             }   
            else   16.             {   
                 System.Console.WriteLine("Negative Integer");   18.             }   
         }   
     }   
}   

Related articles: