C # variable naming rules

Source: Internet
Author: User

When we need to access the information stored in the variable, we only need to use the name of the variable. When you name a variable, follow the rules of the C # language:

Variable names must begin with a letter

Variable names can only consist of letters, numbers, and underscores, and cannot contain spaces, punctuation marks, operators, and other symbols.

Variable names cannot be the same as keyword names in C #. These keywords are given in Appendix A.

The variable name cannot be the same as the library function name in C #.

However, one of the exceptions in C # is that it allows the prefix "@" to be prefixed to the variable name. In this case, we can use the prefix "@" plus the keyword as the name of the variable. This is primarily to avoid conflicts when interacting with other languages. Because the prefix "@" is not actually part of the name, other programming languages will use it as a generic variable name. In other cases, we do not recommend using the prefix "@" as part of the variable name.

Some examples of valid and illegal variable names are given below:

int i; Legal

int No.1; unlawful, containing illegal characters

String Total; Legal

char use; Illegal, same as keyword name

Char @use; Legal

float Main; Illegal, same as function name

Although variable names that meet the above requirements can be used, we still want to give the variable a descriptive name when it is named, so that the written program is easy to understand. For example, the name of a message string can be called S_message, and e90pt is not a good variable name.

We can name multiple variables of the same type in a single statement, such as:

int a,b,c=50,d;

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.