C # variables
A variable is simply the name of a store for the program to manipulate. In C #, each variable has a specific type, and the type determines the memory size and layout of the variable. Values in the range can be stored in memory, and variables can be manipulated in a series of actions.
We have discussed various data types. The basic value types provided in C # can be broadly divided into the following categories:
Type
Example
Integer types SByte, Byte, short, ushort, int, uint, long, ulong, and Char
Floating-point float and double
Decimal type Decimals
Boolean type True or False value, specified value
Nullable types can be null-valued data types
C # allows you to define variables of other value types, such as Enum, and also allows you to define reference type variables, such as class. These will be discussed in a later chapter. In this section, we only study the basic variable types.
Variable definitions in C #
Syntax for variable definitions in C #:
<data_type> <variable_list>;
Here, data_type must be a valid C # data type, which can be a char, int, float, double, or other user-defined data type. Variable_list can consist of one or more comma-delimited identifier names.
Some valid variable definitions are as follows:
int I, J, K;char C, Ch;float F, salary;double D;
You can initialize a variable when it is defined:
int i = 100;
Variable class in C #.
A variable is initialized (assigned) by a constant expression after the equal sign. The general form of initialization is:
Variable_name = value;
Variables can be initialized at the time of declaration (specifying an initial value). Initialization consists of an equal sign followed by a constant expression, as follows:
<data_type> <variable_name> = value;
Some examples:
int d = 3, F = 5; /* Initialize D and f. */byte z = n; /* Initialize Z. */double pi = 3.14159; /* Declare the approximate value of pi */char x = ' x '; /* The value of the variable x is ' x ' */
Initializing variables correctly is a good programming habit, otherwise sometimes the program produces unexpected results.
Take a look at the following example, using variables of various types:
Namespace variabledefinition{ class program { static void Main (string[] args) {short A; int b; Double C; /* Actual initialization */ a = ten; b =; c = a + B; Console.WriteLine ("A = {0}, B = {1}, c = {2}", A, B, c); Console.ReadLine ();}}}
When the above code is compiled and executed, it produces the following results:
A = ten, B = +, c = 30
Accept values from the user
The Console class in the System namespace provides a function, ReadLine (), to receive input from the user and store it in a variable.
For example:
int num;num = Convert.ToInt32 (Console.ReadLine ());
The function Convert.ToInt32 () converts the data entered by the user into an int data type, because Console.ReadLine () accepts only string-formatted data.
Lvalues and Rvalues in C #
Two types of expressions in C #:
Lvalue:lvalue expressions can appear to the left or right of an assignment statement.
An rvalue:rvalue expression can appear to the right of an assignment statement and cannot appear to the left of an assignment statement.
The variable is lvalue, so it can appear to the left of the assignment statement. The value is rvalue and therefore cannot be assigned, and cannot appear to the left of the assignment statement. The following is a valid statement:
int g = 20;
The following is an invalid statement that produces a compile-time error:
10 = 20;
The above is the "C # tutorial" C # variable content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!