C# variables
A variable is just the name of a storage area for the program to operate. In C#, each variable has a specific type, and the type determines the memory size and layout of the variable. Values within the range can be stored in memory, and a range of operations can be performed on the variables.
We have discussed various data types. The basic value types provided in C# can be roughly divided into the following categories:
Type | Example |
---|---|
Integer type | sbyte, byte, short, ushort, int, uint, long, ulong and char |
Floating point type | float and double |
Decimal type | decimal |
Boolean type | true or false value, the specified value |
Null type | Data type that can be null |
C# allows the definition of variables of other value types, such as enum, and also allows the definition of reference type variables, such as class. We will discuss these in later chapters. In this chapter, we only study basic variable types.
Variable definition in C
#The syntax of 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 types. 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 the variable when it is defined:
int i = 100;
Variable initialization in C
#Variables Initialization (assignment) is performed by following the equal sign with a constant expression. The general form of initialization is:
variable_name = value;
Variables can be initialized (specify an initial value) when declared. Initialization consists of an equal sign followed by a constant expression, as shown below:
<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 the program will behave unexpectedly Unexpected results.
Please see the following example, using 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 will produce the following results:
a = 10, b = 20, c = 30
Accepting values from the user
System The Console class in the namespace provides a function ReadLine() for receiving input from the user and Store it into a variable.For example:
int num; num = Convert.ToInt32(Console.ReadLine());
Function Convert.ToInt32() Convert user-entered data to int data type, because Console. ReadLine() only accepts data in string format.
Lvalues and Rvalues in
Two expressions in C#:
##lvalue: The lvalue expression can Appears on the left or right side of an assignment statement.
rvalue: The rvalue expression can appear on the right side of the assignment statement, but cannot appear on the left side of the assignment statement.
int g = 20;The following is an invalid statement and will generate a compile-time error:
10 = 20;