C # basic-variables and constants (3 ),
C # basic-variables and constants (3)
I. Variable operations:
The basic operations of variables include declaring variables and assigning values to variables.
2. Declare variables:
Declared variables are the names and types of specified variables. Declaring a variable is composed of a type and one or more variable names following it. Multiple variables are separated by commas (,). Declaring a variable ends with a semicolon.
Declare an integer variable LS, and then declare the three character variables Strl, Strl, and Str3.
1 int LS; // declare an integer variable 2 string Str1, Str2, Str3; // declare three struct variables at the same time
When declaring a variable, you can also initialize the variable, that is, add the command to assign the initial value to the variable after each variable name.
Declare an integer variable a and assign a value of 927. Then, declare three variable types at the same time and initialize them.
1 int a = 927; // initialize the integer variable a2 string x = "hello", y = "good morning", z = "time"; // initialize the struct variable x, y and z
Pay attention to the naming rules of variable names when declaring variables. In C #, the variable name is a identifier and should comply with the naming rules of the identifier. Variable names are case sensitive. The naming rules for variable names are as follows:
① The variable name can only contain numbers, letters, and underscores;
② The first variable name must be a letter or underline, not a number;
③ Keywords cannot be used as variable names;
④ If a variable name is defined in a statement block, the variable with the same name cannot be defined within the scope of the variable.
Iii. Scope of variables:
The scope of a variable is the code area that can access the variable. Variable scope rules;
1. As long as the class to which a field belongs is in a specific scope, its fields are also in this scope;
2. Local variables exist in the scope before block statements or closed curly braces after the end of the method;
3. Local variables declared in for, while, or similar statements exist in the loop body.
Use the for loop ~ The number of 20 is displayed. Then declare the variable I in the for statement. In this case, I is a local variable and its scope is limited to the for loop body.
1 static void Main (string [] args) 2 {3 // call the for statement to cyclically output the number 4 for (int I = 0; I <= 20; I ++) // for local variable i5 {6 Console in the loop. writeLine (I. toString (); // outputs 0 ~ 20 digits 7} 8 Console. ReadLine (); 9}
Iv. Variable assignment:
In C #, assign a value to the variable using the "=" (equal sign) operator, and assign the value on the right of the equal sign to the variable on the left.
Declare a variable and assign values:
1 int sum; // use a variable 2 sum = 2008; // assign a value to the variable using the value assignment operator "="
Initialization variables are a special way to assign values while declaring variables. If you assign a value to a variable, the right side of the equal sign can also be a variable that has been assigned a value.
Declare the two variables sum and x, assign the sum variable to 927, and assign the sum variable to the variable x.
1 Int sum, x; // declare two variables 2 Sum = 927; // assign the sum variable to 9273 X = sum; // assign the sum variable to the variable x
5. constants:
In C #, you can add the keyword const before a variable declaration to make this value unchangeable.
Constants can only belong to one of the following types: Sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, and string.
Use the keyword const to create a constant, and you must set its initial value when creating a constant. Once set, it cannot be modified. Unlike variables, once a constant is defined, its value cannot be changed within the scope of a constant.
For example, declare a variable MyInt and assign a value of 927, then declare a constant MyInt and assign a value of 112. Finally, assign the variable MyInt to 1039;
1 static void Main (string [] args) 2 {3 int MyInt = 927; // declare an integer variable 4 const int MyWInt = 112; // declare an integer constant 5 Console. writeLine ("variable MyInt = {0}", MyInt); // output 6 Console. writeLine ("constant MyWInt = {0}", MyWInt); // output 7 MyInt = 1039; // assign the variable to 1039 8 Console again. writeLine ("variable MyInt = {0}", MyInt); // output 9 Console. readLine (); 10}
If you try to modify the value of the constant MyWInt, the compiler will see an error message.