@ Symbol in C #

Source: Internet
Author: User

We should pay close attention to many details in programming. If a small letter error can cause the program to fail to run, I have experienced such a thing myself, for issues that should be paid attention to in programming, we need to collect and sort out our own manual frequently. Today we will talk aboutC #
The usage and precautions of the @ symbol in.

  Usage in a string

Character @ indicates that the subsequent string is a "verbatim string" (verbatim string ).

@ Can only act on string constants.

1. Used for file path

 

The following is a reference clip:

String s_filepath = "C: // program files // Microsoft. NET // test.txt ";

Equivalent

 

The following is a reference clip:

String s_filepath = @ "C:/program files/Microsoft. NET/test.txt ";

2. Strings represented by @ can span several rows. It is convenient to write JS or SQL code in CS.

 

The following is a reference clip:

String s_multirows = @ "line1
Line2
Line3 ";
String s_javascript = @"
";

  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.

For example

 

The following is a reference clip:

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: 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.

  • Previous Article: Why is the size of an empty class not zero?
  • Next article: Slicing
  • 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.