C# - VARIABLES
Variables in C# are storage location where the data(Be it a number or a Name) is stored.It is like a container where something is stored.
Example :
Let us go back to our school days. You are in the Maths class and your Maths teacher had
asked you to add two numbers. i.e. She asked you to add the numbers 5 and 6 and tell her
the result.
And you have written the numbers 5 and 6 in your notebook, added them and and told the result
11 to your teacher.
Well! C# takes the same step to add two numbers. The only difference is, you have used one notebook to write and add the numbers.
But C# uses different locations to store each number. And they are called as Variables.
Let us see how Variables are used in C# for addition of two numbers.
C# Code to add two numbers
Example :
public class MyApplication
{
public static void Main(string[] args)
{
int x = 5;
int y = 8;
int z;
z = x + y;
System.Console.WriteLine("The added value is : "+z);
}
}
Output :
The added value is : 13
Now, if you look at the above code, x, y and z are Variables.
And below are the steps involved in addition of the numbers.
-
x is holding the first number 5.
So, in the above line, we are asking C# to write the number 5 to a variable x. int is telling C#, the value to be stored should be a number and not anything else.
Just note that the = in the above code is called assignment operator. It actually means the value 5 at the right should be placed in the storage location/variable x.
-
y is holding the second number 8.
Similarly, we are asking C# to write the number 8 to a variable y.
-
And z would be holding the added value.
Now, an empty variable z is created to store the added value.
-
Then, we are adding the values inside x variable and y variable using + operator
and putting the added value in z variable using the Assignment Operator =.
-
Finally, we are Printing the added value of 5 and 8 on the screen. Which is stored in z.
System.Console.WriteLine("The added value is : "+z);
The above System.Console.WriteLine(...) statement has two sections.
First section has the text inside double quotes ".
And the second section does not have double quotes. And is usually a variable.
And they are separated by a plus sign +.
"The added value is : "+z
The plus sign + in the above statement is a concatenate operator. i.e. It joins the first and the second section to form a meaningful sentence.
And we get the below output :
So far we have seen, how to store a number (i.e. int x = 5) in a Variable.
Now, let us see how to store a string(i.e. It could be a Name or a City) in a Variable.
Storing a string in a Variable
Let us say, we want to store the string Hello World in a Variable(Say x).
And the process of storing a string is same as storing a number, but with a mild difference.
Example :
public class MyApplication
{
public static void Main(string[] args)
{
string x = "Hello World";
System.Console.WriteLine(x);
}
}
Output :
Hello World
So, in the above example, we have used String infront of the Variable x to store a String type value.
string x = "Hello World";
C# Variable Naming Convention
Just like any other language, there is a rule to declare the Variables.
Although, the Variables can be of any names with some restriction.
-
A lower case Variable is different from an upper case Variable.
i.e. The lower case Variable a and upper case Variable A are different.
-
A Variable should never start with a number.
i.e. The Variable with name 2y is never allowed.
-
A Variable can contain alphabets (i.e. A to Z or a to z), numbers (i.e. 0 to 9) or an
underscore (i.e. _).
So, if you have two numbers and need to assign them to two Variables. You can either declare the variables as x and y.
int x = 5
int y = 7
But the preferred way to name the Variables would be firstNumber and secondNumber.
int firstNumber = 5
int secondNumber = 7
But you can never use Variable names with spaces like first number or Variables with a - like first-number.