When you are programming, you often need to save the information that the program runs, so the concept of "variables" is introduced in C #. Some of the values in the program cannot be changed, which is called "constants".
1 variables
The so-called variable is the amount of value that can be changed during a program's operation, and the type of the variable can be any C # data type. Variables of all value types are values that actually exist in memory, that is, a value copy operation is performed when a value is paid to a variable. The variable is defined in the following format:
Variable data type variable name (identifier);
Or
Variable data type variable name (identifier) = variable value;
Where the first definition simply declares a variable and does not assign a value to the variable, the variable uses the default value, the second declaration defines the variable and the variable is initialized, and the value of the variable should be the same as the variable data type. The following code is the use of variables.
int a=10;
Double b,c;
int d=100,e=200;
Double f=a+b+c+d+e;
Explain:
The first line of code: A variable A with an integer type is declared and assigned a value of 10.
The second line of code: Defines two variables of type double, which can be declared in a row when multiple variables of the same type are defined, separated by commas between the variables.
The third line of code: Defines a variable of two integer types and assigns a value to the variable. When multiple variables of the same type are defined and initialized, they can also be run in a single row, separated by commas.
Line four: Add the previously defined variables and assign them to a variable of type double, which is automatically converted to a variable of type double when the sum is calculated.
2 Constants
The so-called constant is the amount of value that cannot be changed during the course of a program's operation. The type of a constant can also be any of the data types of C #. The constants are defined in the following format:
const constant Data type constant name (identifier) = constant value;
Where the const keyword represents a constant, "constant name" is an identifier that identifies the constant. A constant name must be representative and not overly concise or complex. Declarations of constants and variables use identifiers with the following naming conventions:
The. Identifier must begin with a letter or an @ symbol
. Identifiers can only consist of letters, numbers, and underscores, and cannot include special symbols such as spaces, punctuation, and operators
The. Identifier cannot have the same name as a keyword in C #.
The. Identifier cannot be the same as the library function name in C #.
The ' constant value ' type is consistent with the constant data type, and if you define a string type, the constant value should be a string type, or an error will occur.
For example:
const double pi=3.1415926;
Const string version= "Visual Studio 2010";
Explain:
First line: Defines a constant of type double
Second line: a constant that defines a string type.
Once the user has changed the values of these 2 constants in subsequent code, the compilation will find the error and make the code fail to compile.
C # Variables and constants