Understand C # series/Core C #/variables,
Variable variable?
A variable is used to specify a name for a thing. The function of a variable is similar to that of a person's name. When it comes to a name, you will know what it refers.
Variable type?
The variable type indicates the type of the variable. It indicates whether the variable is an integer, a decimal number, a character, an image, a human, or something.
Define Variables
[Variable type] [space] [variable name ]【;]
Assignment
[Variable name] [=] [value ]【;]
The first assignment of a variable is called variable initialization.
Variables must be assigned a value before they are used, that is, they must point to a target. They cannot only have names, but do not know what the names represent.
Assign values when defining variables
[Variable type] [space] [variable name] [=] [value ]【;]
Multiple values are initialized at the same time under the same variable type
[Variable type] [space] [variable name 1] [=] [value 1] [;] [variable name 2] [=] [value 2 ]【;]
When a variable is a field in a class or structure, if it is not initialized,. NET initializes the default value for the variable. When a variable is declared in a method, the compiler reports an error if it is not initialized. Universal variable type var?
The var type is inferred based on the variable initialization value. After the initialization is complete, the variable type is actually clear, and it is essentially a strong type.
Variables of the var type must be initialized. Otherwise, the compiler will not be able to deduce the variable type.
After the var type Initialization is complete, the variable type cannot be changed.
Scope of local variables?
The scope of a variable is the code area that can access the variable. The region is usually divided.
The Code Location for defining variables is different, and the scope is also different.
Variables with the same name cannot exist in a single scope. Otherwise, variables with the same name may exist in different scopes, and they do not interfere with each other.
When the local variables and fields are duplicated
A local variable hides a variable (field) defined under the class with the same name. A variable with the same name points to a local variable.
If you want to locally point to a field with the same name, you can add a special description (this) before the variable name, that is, [this.] [variable name]. this indicates the instance of this class. If the field is a global variable, replace this with the class name.