C# Variables
A variable is nothing but a name for a storage area that a program can manipulate.
In C#, a variable is an identifier used to store and represent data. When declaring a variable, you need to specify its type and can optionally assign it an initial value.
In C#, every variable has a specific type, which determines the size and layout of the variable's memory, the range of values that can be stored, and the set of operations that can be performed on the variable.
We have discussed the various data types. The basic value types available in C# can be roughly categorized as follows:
| Type | Example |
|---|---|
| Integer type | sbyte, byte, short, ushort, int, uint, long, ulong, and char |
| Floating-point | float, double |
| decimal type | decimal |
| Boolean type | true or false values, the specified value |
| Empty string | string |
| null type | nullable data types |
C# allows defining variables of other value types, such as enum, and also allows defining reference type variables, such as class. We will discuss these in later chapters. In this chapter, we focus only on basic variable types.
C# 4.0 introduced the dynamic type (dynamic), which allows the type of a variable to be inferred at runtime. This is useful in some special cases, but it is usually better to use static types for better performance and compile-time type checking.
dynamic dynamicVariable = "This can be any type";
Variable definition in C#
The syntax for variable definition in C#:
;
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 comma-separated identifier names.
Some valid variable definitions are as follows:
int i, j, k;
char c, ch;
float f, salary;
double d;
You can initialize at the time of variable definition:
int i = 100;
Variable Naming Rules
In C#, variable naming must follow some rules:
- Variable names can contain letters, digits, and underscores.
- Variable names must start with a letter or underscore.
- Variable names are case-sensitive.
- Avoid using C# keywords as variable names.
int myVariable = 10;
string _userName = "John";
Variable initialization in C#
A variable is initialized (assigned a value) by following an equal sign with a constant expression. The general form of initialization is:
variable_name = value;
A variable can be initialized at the time of declaration (given an initial value). Initialization consists of an equal sign followed by a constant expression, as shown below:
= value;
Some examples:
int d = 3, f = 5; /* 初始化 d 和 f. */
byte z = 22; /* 初始化 z. */
double pi = 3.14159; /* 声明 pi 的近似值 */
char x = 'x'; /* 变量 x 的值为 'x' */
Correctly initializing variables is a good programming practice; otherwise, the program may sometimes produce unexpected results.
Look at the following example, which uses variables of various types:
Example
using
System
;
namespace
VariableDefinition
{
class
Program
{
static
void
Main
(
string
[
]
args
)
{
short
a
;
int
b
;
double
c
;
/* actual initialization */
a
=
10
;
b
=
20
;
c
=
a
b
;
Console
.
WriteLine
(
"a = {0}, b = {1}, c = {2}"
, a, b, c
)
;
Console
.
ReadLine
(
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
a = 10, b = 20, c = 30
Accepting values from the user
System
namespace
Console
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());
function Convert.ToInt32() Convert the data entered by the user to the int data type, because Console.ReadLine() only accepts data in string format.
Lvalues and Rvalues in C#
Two kinds of expressions in C#:
-
lvalue: an lvalue expression can appear on either the left or right side of an assignment statement.
-
rvalue: an rvalue expression can appear on the right side of an assignment statement, but not on the left.
A variable is an lvalue, so it can appear on the left side of an assignment statement. A numeric value is an rvalue, so it 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 that produces a compile-time error:
10 = 20;