• Now Online : 61
  • admin@codemyne.net

Introduction

Variables : A Variable is an identifier that denotes a storage location in the memory. By using a variable's name in your program you are, in fact, referring to the information or data stored at that location. Unlike constants that remain unchanged during the execution of a program, a variable may take different values at different times during the execution of a program. Every variable that has a type that determines what values can be stored in that variable.
You can give any name to a variable but it should be meaningful because it makes code more readable. Here are some examples of meaningful variable names:
  • salary
  • height
  • name
  • age
  • total_marks

There are certain rules for naming C# variables:

  • The name may consist of letters, digits, and the underscore character(_)
  • They must not begin with a digit
  • Uppercase and lowercase characters are distinct; for example, salary and SALARY are two different variables
  • It should not be a keyword
  • White spaces are not allowed
  • Variable names can be of any length

The table shows some examples of valid and invalid C# variable names:

Variable Name Status
expenditure Applicable
Net_amount Applicable
monthly_cost Applicable
_2011_tax Applicable, but not advised
Total#amount Not Applicable, contains the illegal character #
single Not Applicable,it is a keyword
9phone Not Applicable, first character is a digit

Declaration of Variables: Variables are the names of storage locations for data. After giving variables a meaningful name, you must declare them to the compiler. In simple words, a compiler is software that converts a source code written in a programming language to machine language which a computer can understand. Declaration does three things:
  • It tells the compiler the name of the variable
  • It tells about the type of data, a variable holds
  • The place of the declaration in the program decides the scope of the variable

A variable must be declared before it is used in the program. You can use any meaningful name for a variable using any datatype.
Here is the variable declaration syntax:

datatype varable1, variable2, variable3,....variableN;

If the variables to be declared are more than one and of the same data type then we can declare them in the same line separated by comma. A code line in C# always ends with semicolon.Here are some examples of legal variable declaration:

                                                    float length;
                                                    int age;
                                                    char b1,b2;
                                                    double pi;
                                                    decimal money;
Initialization of Variables: After the declaration of variable you need to initialize its value. The condition for initialization is that a variable must be assigned a value before being used in the program. It is not mandatory to initialize a variable at the time of declaration; this is totally a programmer's choice. The syntax for initialization is:
    variablename = value;
    (or)
    variable1 = variable2 = variable3 = value;

If we initialize a variable at the time of declaration then the syntax is:

    datatype variablename = value;
    (or)
    datatype variable1 = value, variable2 = value;
Here are some examples of initializing a variable:
                                                    int age = 23;
                                                    bool status = false;
                                                    float height = 164.5f;
                                                    char sex = 'F';
                                                    int a = 4, b = 6;
                                                    decimal d = 2.34M;
Scope of Variables: The scope of variables is the area of code within which the variables can be accessed. This all depends upon the place where we have declared a variable and what type it is using. Mainly there are three categories of variables:

Local Variables: The scope of local variables is within the method where it has been declared. for example:
                                                    class class1
    {
                                                    public void mymethod()
    {
                                                    int x = 10;
                                                    int y = 10;
    ...............
    ...............
    }
    }
In the above code x and y are local variables whose scope is within the method mymethod. During the execution of a program the scope of local variable is lost after the control exits the method body. If you try to access these variables outside this method you will get an error indicating that no such variables outside this method you will get an error indicating that no such variable is declared.

Static Variables: Static variables are the variables which retain their value throughout the program. We access the value of static variables through the class name. For example:
                                                    class class1
    {
                                                    static int x = 10;
                                                    public void mymethod()
    {
    Console.Writeline(Class1.x);
    .........................
    .........................
    }
    }
In the above code we are accessing the static variable through class name .i.e. through the code Class1.x

Instance Variables: The main difference between local and instance variables is that local variables are directly accessed by their name and instance variables are accessed using the object of the class as you can see in the code given below:
                                                    public class program
    {
                                                    int x = 60;
                                                    public static void main(string[] args)
    {
    program obj = new program();
    Console.Writeline(obj.x);
                                                    int x = 10;
    Console.Writeline(x);
    Console.Read();
    }
    }
Here x declared outside the method is an instance variable while the other declared inside the method is a local variable. Instance variable is accessed by creating object of a class that is obj.
Constants: Constants are those values which are fixed and do not change during the execution of the program. For example, as we all know that value of PI is fixed so we can declare it as constant, or any other value that is required not to be changed can be declared as constant. Here is the syntax for declaring a constant:
                                                    const datatype constantname = value;
You must declare and initialize a constant at the same time as in the following case or else an error will occur:
                                                    const float PI = 3.14;
                                                    const int count = 4;
So, these values cannot be changed during the execution of program. The const keyword is used to declare a constant and the rest of the code is same like in case of declaring variables. A constant can also be initialized using an expression such as:
                                                    const int length = 10;
                                                    const int breadth = length * 2;
You cannot use a non-constant value in an expression which is used by a constant such as:
                                                    int length = 10;
                                                    // This is illegal statement because it is using a non-constant value
                                                    const int breadth = length * 2;
The above code declares a variable length which has the value 10 and a constant width breadth of int data type is declared which is initialized a value of the product of the length and the number 2 which results in an error since you can not use a non-constant value in an expression while initializing a value to a constant.

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment