![]() |
VOOZH | about |
A variable in C# is a named memory location used to store data during program execution. The stored value can be accessed, modified, and reused throughout the program.
Each variable consists of three components:
3 7
Declaration means defining the data type and name of a variable. At this stage, the variable is created but no value is assigned.
dataType variableName;
Example:
int x;
double price;
char grade;
Here:
Initialization means assigning a value to a variable after it has been declared. Example:
int x;
x = 5;
In this example, the variable x is declared first and later assigned the value 5.
A variable can also be declared and initialized in a single statement.
int y = 7;
There are two basic ways for initialization as mentioned below:
In compile-time initialization, the value of the variable is assigned when the program is written. The value remains fixed during execution. Example:
Value of x is 32 Value of y is 0
Here, the values of x and y are known before the program runs.
In run-time initialization, the value of the variable is assigned during program execution. The value may come from user input, function calls, or program logic. Example:
Input:
Enter a number: 45
Output:
Value of num is 45
Explanation: Console.ReadLine() reads input from the user and the entered value is stored in the variable num.