May 11, 2021 C#
2. The variable definition in C#
3. The variable initialization in C#
A variable is simply the name of a store for program operations. E ach variable has a specific type in C# that determines the memory size and layout of the variable. Values in range can be stored in memory and a series of operations can be performed on variables.
We've discussed various data types. The basic types of values provided in C# can be broadly divided into the following categories:
type | Example |
---|---|
Integer type | Sbyte, Byte, Short, Ushort, Int, Uint, Long, Ulong and Char |
Floating point | Float and Double |
Decimal type | decimal |
Boolean type | TRUE or FALSE value, specified value |
Empty type | Data types that can be empty |
It allows you to define variables of other value types, such as enum, and also to define reference type variables, such as class. T hese will be discussed in a later section. In this section, we will only look at the basic variable types.
The syntax of the variable definition in C#:
<data_type> <variable_list>;
Here, data_type must be a valid C# data type, which can be char, int, float, double, or other user-defined data type. variable_list can consist of one or more identifier names separated by commas.
Some valid variable definitions are as follows:
int i, j, k;
char c, ch;
float f, salary;
double d;
You can initialize variable definitions:
int i = 100;
Variables are initialized (assigned) by following an equal sign with a constant expression. 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; /* 初始化 d 和 f. */
byte z = 22; /* 初始化 z. */
double pi = 3.14159; /* 声明 pi 的近似值 */
char x = 'x'; /* 变量 x 的值为 'x' */
It is a good programming habit to initialize variables correctly, otherwise sometimes programs produce unexpected results.
Take a look at the following example, which uses various types of variables:
namespace VariableDefinition
{
class Program
{
static void Main(string[] args)
{
short a;
int b ;
double c;
/* 实际初始化 */
a = 10;
b = 20;
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 = 10, b = 20, c = 30
In the namespace
ConsoleThe class 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 to an int data type because Console.ReadLine() only accepts data in string format.
Two expressions in C#:
The lvalue :lvalue expression can appear on the left or right side of the assignment statement.
The rvalue :rvalue expression can appear on the right side of the assignment statement, not on the left side of the assignment statement.
The variable is lvalue, so it can appear to the left of the assignment statement. T he value is rvalue, so it cannot be assigned and cannot appear to the left of the assignment statement. Here's a valid statement:
int g = 20;
Here is an invalid statement that produces a compile-time error:
10 = 20;