C # basic learning notes -- C # variables,
Variables can be used in three steps: Declaration, value assignment, and usage.
Syntax for declaring variables: data type variable name;
Syntax for assigning values to variables: variable name = value;
1 namespace Test 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 int age; // declare the variable, type: int, variable name: age 8 age = 18; // assign the variable age to 18 9 Console. writeLine ("My years" + age = "years old"); // print age10 age = 19; // modify the variable age value 11 Console. writeLine ("My next year" + age + "years old"); // print age12} 13} 14}
The running result is:
The declaration and value assignment of variables can be completed at the same time. The first two lines in the Main () method of the code above can be rewritten:
Int age = 18; // declare and assign values
Exercise questions:
1 static void Main (string [] args) 2 {3 int age; // age 4 age = 18; 5 age = 20; 6 age = 20 + 1; 7 Console. writeLine ("I am {0} years old", age); 8 age = age-2; 9}
This code prints: I am 21 years old.
The above is taken from MOOC course C # getting started with development