This article summarizes the use of @ in C #, which has a good reference value for C # programming. Specific as follows:
Usage in a string
1. People who have learned C # know that the string constants in C # can begin with @, so the advantage is that the escape sequence is "not" processed and output "as is"-that is, we do not need to add \ (anti-skew) to the escape character, which makes it easy to coding. Such as
String filePath = @ "C:\Docs\Source\a.txt"//rather than "c:\\docs\\source\\a.txt"
2. If you want to include a double quote in a string that is caused by @, you need to use two pairs of double quotes. You cannot use \ to escape the good quotes at this time, because the escape use here has been dropped by the @ "mask". Such as
@ "" "ahoy!" "cried the Captain." The output is: "ahoy!" cried the captain.
This is a bit like the single quote constant processing in sql:
DECLARE @msg varchar SET @msg = ' ahoy! ' cried the captain. '-Output: ' ahoy! ' cried the captain.
3.@ will recognize newline characters.
In fact, this feature, I do not know how to describe, just accidentally found, first look at the following code:
string script = @ "<script type=" "Type/javascript" "> Function DoSomething () {} </script>";
This code in the CS file to write JS, the structure is very clear, the normal situation we are so coding:
String script2 = "<script type=\" type/javascript\ ">function dosomething () {}</script>";
Or:
String script3 = "<script type=\" type/javascript\ ">" + "function dosomething () {" + "}</script>";
Usually we will choose the latter, because the JS code is generally longer, or the method body is large, or need to connect other variables, so that the structure is clearer.
Note: If "stitching" is a lot of times, you should consider using StringBuilder to help improve performance .
There is also a scenario, and it is common to splice SQL statements in the program, such as
Private Const string Sql_ins_user = @ "INSERT into T_user ([UserName], [Password], Email) VALUES (@UserName, @Password, @Email) ";
This is like writing stored procedures in general, maintaining fairly high code clarity. However, we need to focus on a problem: string length look at the following test code:
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, @Ema IL) "; static void Main (string[] args) { Console.WriteLine (sql_ins_user1. Length); 126 Console.WriteLine (Sql_ins_user2. Length); Console.WriteLine (Sql_ins_user3. Length); 86}
Here you can see that the length of the three string is different, 14=126-112 and 26=112-86, notice that in the Code Editor, after the first newline symbol in Sql_ins_user1, I indent 13 spaces (before insert), and
After the first newline symbol in Sql_ins_user2, I indent 25 spaces (before values),
Well, add a newline character, just good 14 and 26
Writing this code, while improving the clarity and simplicity of the code, brings another problem in no Line: character length!
In many scenarios we want the string to be as short as possible, such as sending SQL statements to the database via ADO.
So be careful with it!
Usage in two identifiers
In the C # specification, @ can be the first character of an identifier (class name, variable name, method name, and so on) to allow the reserved keyword in C # as its own defined identifier.
The following code:
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 that the@ appears in the identifier, but not as part of the identifier itself.
Thus, in the example above, a class is defined, containing a method named Static, and a parameter name for the bool argument.
This facilitates cross-language porting. Because a word is reserved as a keyword in C #, it may not be in another language.