Use the @ symbol to explain how to use reserved words in C # as variable names

Source: Internet
Author: User

In C and C ++, we know there is no way to start a variable named int, because this is a reserved keyword for C/C ++, such a variable name cannot tell whether it is an int type or an int variable.
In fact, there is also such a rule in C # That a variable name cannot be a reserved keyword. For example:

In C/C ++, we can't do anything about it. You must start a non-int type, such as adding an underscore ..
Of course, this can also be done in C.

@
I read "C #3.0 in a nutshell, 3rd Edition" today to know that there was another way to add the @ symbol (I saw another @ symbol ):CopyCodeThe Code is as follows: Class @ int
{
Static void main (string [] ARGs)
{
Console. writeline (@ string. @ long );
}
}
Class @ string
{
Public static int @ long = 3;
}

Of course, the @ symbol can be added before the reserved word or before any other valid variable name:Copy codeThe Code is as follows: String @ demostr = string. empty;

Note:In the compiler, the @ symbol is automatically removed, that is, demostr and @ demostr are considered to be the same variable in the compiler.

Although C # provides this mechanism to create a variable name with reserved words, I personally think this is a bad code style. This method may confuse others who do not know this feature.

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.