SoFunction
Updated on 2025-03-06

Summary of C# variable naming rules

variable:

1. Function: allows us to store data in computers

2. Syntax: Variable type Variable name = assignment;

3. Commonly used data types:

  • int   Integer type Value range: maximum 2147483647; minimum -2147483648
  • double  Decimal type Value range: maximum 1.79769e+308; minimum -1.79769e+308
  • string    string  "   "
  • char    Character   '
  • decimal     Money type
  • bool   We use the bool type to describe right or wrong.
  • true    Right
  • false   Wrong

4. Usage rules

  • Statement first
  • In assignment
  • Last used

5. Features

  • Repeated definitions are not allowed
  • But you can repeatedly assign values

6. Naming Rules

Must start with letters, followed by any letters, numbers, or underlined

Naming Specifications

  • Camel Naming Specifications: Camel Requirements for the first letter of the first word to be lowercase and the first letter of each other word to be uppercase. Mostly used to name variables
  • Pascal requires that the first letter of each word must be capitalized, and is mostly used to name classes or methods.

Note: The variable name cannot be a keyword in VS

7. Scope

Local variables
In the method, all variables we declare are called local variables. The scope of local variables is the current method

Member variables
In a class, the variable we declare is called a member variable. The scope of the member variable is the current class.

The so-called scope refers to the scope where we can use this variable.

C# variable naming rules (naming specifications)

C# naming rules are set to unify the entire program code to enhance its readability. Each unit will write a document on coding specifications before developing a software.

There are two common naming methods, one is Pascal nomenclature (Pascal nomenclature), and the other is Camel nomenclature (camel nomenclature).

Pascal nomenclature refers to the capitalization of the first letter of each word; Camel nomenclature refers to the capitalization of the first word starting from the second word.
1) Variable naming rules
The naming rules for variables follow the Camel nomenclature and try to use English words that can describe the function of variables. For example, the variables that store students' names can be defined as name or studentName, etc. In addition, it is not recommended to have too long variable names. It is best to have 1 word and no more than 3 words at most.
2) Constant naming rules
To distinguish it from a variable, all letters of the word that defines a constant are usually capitalized. For example, defining the value of n of the circle area can be defined as a constant to ensure that the values ​​used in the entire program are unified, and can be directly defined as PI.
3) Class naming rules
The naming rules of classes follow Pascal nomenclature, that is, the capitalization of the first letter of each word. For example, define a class that stores student information, which can be defined as Student.
4) Interface naming rules
The naming rules of the interface also follow the Pascal nomenclature, but usually start with I and capitalize the initial letter of each word after it. For example, define an interface that stores value comparison operations, which can be named ICompare.
5) Method naming rules
The naming of methods follows Pascal nomenclature and is generally named using verbs. For example, implementing a method to add user information operation, you can name it AddUser.

In C# language, there are many objects in addition to the above, but the naming rules are similar. When other objects are involved, the naming rules will be explained again.

This is the end of this article about the summary of C# variable naming rules. For more related C# variable naming content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!