@ Usage in C # string

Source: Internet
Author: User

1. 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 escape characters. For example
String filePath = @ "c: DocsSourcea.txt"; // rather than "c: DocsSourcea.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 the escape quotes, because the escape usage here has been blocked. For example
@ "Ahoy! "" Cried the captain. "; // the output is:" Ahoy! "Cried the captain.
It is a bit like the single quotation mark constant Processing Method in SQL:
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:
String script = @"
<Script type = "" type/javascript "">
Function doSomething ()
{
}
</Script> ";

Write js in the cs file, and the structure is very clear. Normally we are coding like this:
String script2 = "<script type =" type/javascript "> function doSomething () {}</script> ";
// Or
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
Private const string SQL _INS_USER = @"
Insert into t_User ([UserName], [Password], Email)
VALUES (@ UserName, @ Password, @ Email )";
 
However, we need to pay attention to the following question: String Length
See 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, @ 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
}
We can see that the lengths of the three strings are different, 14 = 126-112 and 26 = 112-86. In the code editor, SQL _INS_USER1
After the first line break in, 13 spaces are indented (before INSERT), while
After the first line break in SQL _INS_USER2, 25 spaces are indented (before VALUES ),
Then, add a line break, just 14 and 26

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.