The examples in this article summarize the usage of @ in C #, which has good reference value for C # program design .,

Source: Internet
Author: User

The examples in this article summarize the usage of @ in C #, which has good reference value for C # program design .,

The details are as follows:

Usage in a string

1. anyone who has learned C # knows that the String constant in C # can start with @. This advantage is that the escape sequence "not" is processed and output as is, that is to say, we can easily coding without adding \ (reversed) to escape characters. For example,

 
1 string filePath = @"c:\Docs\Source\a.txt" // rather than "c:\\Docs\\Source\\a.txt"

2. To include a double quotation mark in a string caused by @, two pairs of double quotation marks are required. At this time, you cannot use \ to escape the double quotation marks, because the escape purpose of \ here has been blocked. For example,

 
1 @"""Ahoy!"" cried the captain."// Output: "Ahoy! "Cried the captain.

This is a bit like the single quotation mark constant Processing Method in SQL:

 
12 DECLARE @msg varchar(100) SET @msg = ''Ahoy!'' cried the captain.'-- The output is :'Ahoy!' cried the captain.

3. @ recognizes line breaks

In fact, I don't know how to describe this feature. I just accidentally discovered it. Let's take a look at the following code:

 
123456 string script = @" <script type=""type/javascript""> function doSomething() { } </script>";

This code writes js in the cs file, and the structure is very clear. Normally we are coding like this:

 
1 string script2 = "<script type=\"type/javascript\">function doSomething(){}</script>";

Or:

 
1234 string script3 = "<script type=\"type/javascript\">" + "function doSomething(){ " + "}</script>";

We usually select the latter because the js Code is generally long, the method body is large, or other variables need to be connected, so that the structure is clearer.

Note: If the number of "splices" is large, you should consider using StringBuilder to improve performance.

It is also common to splice SQL statements in a program, such

 
123 private const string SQL_INS_USER = @" INSERT INTO t_User([UserName], [Password], Email)   VALUES(@UserName, @Password, @Email)";

This is like writing a stored procedure, maintaining a high degree of code clarity. However, we need to pay attention to the following test code:

 
123456789101112131415 private const string SQL_INS_USER1 = @"   INSERT INTO t_User([UserName], [Password], Email)   VALUES(@UserName, @Password, @Email)";  private const string SQL_INS_USER2 = @"INSERT INTO t_User([UserName], [Password], Email)   VALUES(@UserName, @Password, @Email)";  private const string SQL_INS_USER3 = @"INSERT INTO t_User([UserName], [Password], Email) VALUES(@UserName, @Password, @Email)" static void Main(string[] args) {   Console.WriteLine(SQL_INS_USER1.Length);  // 126    Console.WriteLine(SQL_INS_USER2.Length);  // 112   Console.WriteLine(SQL_INS_USER3.Length);  // 86 }

Here we can see that the length of the three strings is different, 14 = 126-112 and 26 = 112-86. Note that in the Code Editor, after the first line break symbol in SQL _INS_USER1, I indent 13 spaces (before INSERT), while
After the first line break in SQL _INS_USER2, I indented 25 spaces (before VALUES ),
Then, add a line break, just 14 and 26

Writing code in this way improves the clarity and simplicity of the code, but it creates another problem in the absence of lines: character length!
In many scenarios, the shorter the string, the better. For example, you can use ADO. NET to send SQL statements to the database for execution.
So use it with caution!

Usage in binary identifiers

In the C # specification, @ can be the first character of the identifier (Class Name, variable name, method name, etc.) to allow the reserved keyword in C # as its own defined identifier.

The following code:

 
123456789101112131415 class @class{   public static void @static(bool @bool) {    if (@bool)      System.Console.WriteLine("true");    else     System.Console.WriteLine("false");   }   } class Class1 {   static void M() {    cl\u0061ss.st\u0061tic(true);   } }

Note: Although @ appears in the identifier, it is not part of the identifier.
Therefore, the preceding example defines a class and contains a method named static and a parameter named bool.

This facilitates cross-language transplantation. Because a word is reserved as a keyword in C #, but it may not be in other languages.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.