Variables are like a storage unit that stores the values of various data-types present in a C# programming language. The types are different in C# and accordingly, the memory sizes are allocated to those types to store data values.
Consider an example where you have declared a variable of type
int that means that the int variable will be allocated to a memory where it will integer values and some specific operation can be performed.
And for operation, consider the variable int that you described and since it is an integer the operation like addition and subtraction, multiplication, or modulus is performed on those integer operands.
C# consists of various types variables provided by its data-types:
- int: It stores integers values such as 65 or -65.
- double: It stores floating-point numbers such as 15.34 or -15.34.
- char: It stores the character in a single-quotes such as ‘s‘.
- string: It is for storing the string in double quotes such as “simple code“
- bool: It states two values: true and false.
data-type variable_name = value;
How to declare variable in C#?
It is simple to declare a variable and it must be declared before the use of that variable. You need to specify the data-type for the variable and also give it a unique name.
As mentioned above data-types can be of any type: int, float, double, char, string or bool, according to the need.
Let us see an example how you can declare it in a program.
How to initialize a variable in C#?
Initialization means giving the variable an initial value to start with. We can initialize the variable at the declaration part such as:
int number = 45;.
The syntax is simple, first, start with the data-types and unique name the same as a declaration and then assign it a value with equal sign ‘=’.
<data-types> <variable_name> = <data_value>;
The data value is always assigned at the right-hand sign of the assignment operator ‘=‘. Let us see some valid examples of how you can initialize a variable.
int num1 = 45, num2 = 67;
double pi = 3.14159;
char ch = 's';
In the above example as you can see the first line contains two initialization which is possible considering you ant the two variable n you program to be an integer type.
Sometimes in a program you need to the value from the user and store it in a variable and use those variable at run time in a programs.
To accept the inputs from the user, C# provides a particular function named as ReadLine() that take the inputs and store it in a variable. It is present in the console class in the System.
How to use it in a program:
number = Convert.ToInt32(Console.ReadLine());
The above example specifies that the variable is declared and it takes the user input and places it in a
number variable. Conversion of inputs to integer data-type is done by
Convert.ToInt32 and acceptance of input is completed by
Console.ReadLine() in the program.
C# defines two kinds of expressions:
- lvalue: The expression may appear on either on the right-hand side or the left-hand side of the assignment operator.
- rvalue: The expression can only appear on the right-hand side but not on the left-hand side of the assignment operator.
Variables are lvalue expression as it can appear on the right-hand side as well as on the left-hand side of an equal sign’=’ in a program. However numeric values are rvalues as it can only be assigned to the right-hand side of the ‘=’ value.
This is because the assignment operator(=) always assigns the right-hand side value to the left-hand side and we cannot assign something to a numeric value that is why they are the rvalue.
Look at the below example that shows which are valid and which are not.
int num1, num2; //variable declaration
num1 = 5; //valid
num2 = num1; //valid
5 = 45; //invalid
There are few rules you need to follow which naming the variables such as it must be unique, it has to be letters, digits, or the underscore
_. Variables are case sensitive that is Number and number are considered different variables.