Use and precautions of "@" symbol in C #

Source: Internet
Author: User
Many of the details of programming are very important to us, or a small letter error will cause the program can not run, I have experienced such a thing, for programming should pay attention to the problem, we should often collect and organize their own a handbook, today we talk about the use of the @ symbol in C # and notice matters.

Many of the details of programming are very important to us, or a small letter error will cause the program can not run, I have experienced such a thing, for programming should pay attention to the problem, we should often collect and organize their own a handbook, today we talk about the use of the @ symbol in C # and notice matters.

The usage in a string

The character @ indicates that the string that follows is a "verbatim string" (verbatim string).

@ can only function on string constants.

1. For file path

The following are the referenced contents:
String S_filepath = "C:\\Program Files\\microsoft.net\\test.txt";


Equivalent

The following are the referenced contents:
String S_filepath =@ "C:\Program files\microsoft.net\test.txt";


2. A string that is represented by @ can span several lines. It is more convenient to write JS or SQL code in CS.

The following are the referenced contents:
String s_multirows = @ "Line1
Line2
Line3 ";
String s_javascript = @ "
";


The use of the two identifiers

In the C # specification, @ can act as the first character of an identifier (class name, variable name, method name, and so on) to allow a reserved keyword in C # as its own defined identifier.

Such as

The following are the referenced contents:

Class @class
{
public static void @static (bool @bool) {
if (@bool)
System.Console.WriteLine ("true");
Else
System.Console.WriteLine ("false");
}
}
Class Class1
{
static void M () {
Clu0061ss.stu0061tic (TRUE);
}
}


Note that @ is not part of the identifier itself, although it appears in the identifier.

Therefore, the above example defines a class named class and contains a method named Static, and a parameter name for the formal parameter of bool.

This provides a convenient way to migrate across languages. Because, a word in C # as a reserved keyword, but in other languages may not be.



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.