(original) C # learning Note 03--variable and expression 03--variable 02--variable name

Source: Internet
Author: User

3.3.2 The name of the variable

The basic variable naming rules are as follows:

The first character of a variable name must be a letter, underscore (_), or @.

The subsequent characters can be letters, underscores, or numbers.

In addition, there are some keywords that have specific meanings for the C # compiler, such as the using and namespace keywords that appear earlier. If you use one of the keywords incorrectly, the compiler will produce an error and we'll know that something went wrong, so don't worry.

Remember,C # is case-sensitive , so be careful not to forget to use the correct casing when declaring variables.

Naming conventions

Recently, the most popular system is the so-called Hungarian notation. This system adds a lowercase prefix to all variable names, indicating their type. For example, if the type of the variable is int, just precede its name with I (or n), such as Iage. Using this system, it is easy to see what kinds of variables are.

Currently, there are two naming conventions in the. NET framework namespace, called Pascalcase and CamelCase. The case used in the name indicates their purpose. They are applied to names that consist of multiple words, and each word in the specified name is lowercase except for the first letter capitalized. In the CamelCase rule, there is also a rule that the first word starts with a lowercase letter.
Here is the CamelCase variable name:
Age
FirstName
Timeofdeath
Here is the pascalcase variable name:
Age
LastName
Winterofdiscontent
Microsoft recommends that you use the CamelCase rule for simple variables, and pascalcase for more advanced naming. Finally, note that many previous naming systems often use underscore characters as delimiters between individual words in a variable name, such as Yet_another_variable. But this usage has now been eliminated.

Note

The personal experience of naming is:

Global variables: g_, such as global int variable, g_imaxsize = 10;

Variables visible to all methods in the local. cs file: M_, for example: M_imaxsize = 10;

The local variables in the function are used, for example: Imaxsize = 10;

The advantage of this kind of naming is that you can know the scope of the variable by looking at the name, and you can know the type of the variable by looking at the tag (in the example above: i), and by the name of the variable and by the word composition (maxSize), you can know the meaning of the variable expression.

Of course, this is only a personal habit, the most important thing is that everyone should develop their own set of norms, and later their own maintenance is very convenient;

(original) C # learning Note 03--variable and expression 03--variable 02--variable name

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.