C# Variables
A variable is just the name of a storage area for program operations. 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:
types
examples
integer types 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#
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 at variable definition time:
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 practice to initialize variables correctly, otherwise sometimes the program will behave unexpectedly Unexpected results.
Look at 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 produces the following results:
a = 10, b = 20, c = 30
Accepting values from the user
The Console class in the System namespace provides a function ReadLine() for receiving input from the user and storing 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 the int data type, because Console.ReadLine() only accepts characters Data in string format. Lvalues and Rvalues in
C# Two expressions in
C#:
lvalue: An lvalue expression can appear 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.
variables are lvalues, so they can appear on the left side of assignment statements. Numeric values are rvalues and therefore cannot be assigned and cannot appear on the left side of an assignment statement. The following is a valid statement:
int g = 20;
The following is an invalid statement, which will generate a compile-time error:
10 = 20;
The above is the content of C# variables in [C# Tutorial], for more related information, please Follow the PHP Chinese website (www.php.cn)!