C# Variables
Variables in C# are the same as variables in mathematics. Before we understand what is a variable, let's understand the expressions. The followings are examples of expressions.
10 + 20
5 * 2
10/2
The result of the above expressions are fixed e.g. 10 + 20 = 30
, 5 * 2 = 10
and 10/2 = 5
. Now, consider the following expressions.
x + 20
y * 2
z/2
The result of the above expression depends on the value of x
, y
and z
. For example, if x = 5
then the result of x + 20
would be 25
and if x = 20
then the result of x + 20
would be 40
. In the same way, the result of y * 2
depends on the value of y
and the result of z/2
depends on the value of z
. Here, x
, y
, z
are called variables because their values can be changed.
The same concept is used in C#. In C#, a variable stores a value of the specific data type. It can store a numeric, char, string, or other types of value. You can declare and assign a value to a variable like int x = 5;
where int
is the data type, x
is the name of a variable, =
is an operator that assigns the value to a variable, and 5
is the integer value assigned to a variable x
.
<data type> <variable name> = <value>;
The following declares and initializes a variable of an int
type.
int num = 100;
Above, int
is a data type, num
is a variable name (identifier). The =
operator is used to assign a value to a variable. The right side of the =
operator is a value that will be assigned to left side variable. Above, 100 is assigned to a variable num
.
The following declares and initializes variables of different data types.
int num = 100;
float rate = 10.2f;
decimal amount = 100.50M;
char code = 'C';
bool isValid = true;
string name = "Steve";
The followings are naming conventions for declaring variables in C#:
- Variable names must be unique.
- Variable names can contain letters, digits, and the underscore
_
only. - Variable names must start with a letter.
- Variable names are case-sensitive,
num
andNum
are considered different names. - Variable names cannot contain reserved keywords. Must prefix
@
before keyword if want reserve keywords as identifiers.
C# is the strongly typed language. It means you can assign a value of the specified data type. You cannot assign an integer value to string type or vice-versa.
int num = "Steve";
Variables can be declared first and initialized later.
int num;
num = 100;
A variable must be assigned a value before using it, otherwise, C# will give a compile-time error.
int i;
int j = i; //compile-time error: Use of unassigned local variable 'i'
The value of a variable can be changed anytime after initializing it.
int num = 100;
num = 200;
Console.WriteLine(num); //output: 200
Multiple variables of the same data type can be declared and initialized in a single line separated by commas.
int i, j = 10, k = 100;
Multiple variables of the same type can also be declared in multiple lines separated by a comma. The compiler will consider it to be one statement until it encounters a semicolon ;
.
int i = 0,
j = 10,
k = 100;
The value of a variable can be assigned to another variable of the same data type. However, a value must be assigned to a variable before using it.
int i = 100;
int j = i; // value of j will be 100
Variables can be used in an expression and the result of the expression can be assigned to the same or different variable.
int i = 100;
int j = i + 20; // j = 120
i = 200;
j = i + 20;// j = 220
i = 300;
Console.WriteLine("j = {0}", j);// j = 220
In the above example, value of j
depends on the value of i
. You must re-execute expression each time you change the value of i
; otherwise, value of j
would not change based on the value of i
.
In C#, variables are categorized based on how they store their value in memory. Variables are categorized into value type or reference type or pointer type variables.
It is not necessary to specify the specific type when declaring variables. Use the var
keyword instead of a data type. Learn about it next.