Part 4 Global,Local,Instance,Static Variable

A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a specific type, which determines the size and layout of the variable's memory the range of values that can be stored within that memory and the set of operations that can be applied to the variable.
we will discuss

Local Variables 

Global Variables

Instance Variable Static Variable

    1. Local Variable :

     local variable is used to declare inside the method in which it is declared. They can be used only by statements that are inside that Method.


    class Fullstack 
    {
           //Main method
          public static void Main()
          {
                   int  a;
                   a=24;
            / /Write to console
            Console.WriteLine("Value is" +a);
         }
    }



    2. Global Variables


    A global variable is a variable accessible anywhere.C# is an OOP language and does not support global variables directly. The solution is to add a static class containing the global variables. this is important to understand but when i developed a web based application then i will explain in very great .


    3. Instance Variable 

    A instance variable is used to declare outside the method and are accessible to all the methods

    class Fullstack 
    {
            int  a; //Instance Variable             
           //Main method
          public static void Main()
          {
                Fullstack  programer=new Fullstack ();
                programer.a = 24;
            / /Write to console
            Console.WriteLine("Value is" + programer.a );
         }
    }


    4. Static Variable 

    is known as class variable. It is declared outside the method. Static variable is called using class name. We cannot call static variable with object.Static variables are used to define constants because their values can be retrieved by invoking the class without creating an instance of it.



     class Program

        {

            public static int i;

            public static void display()

            {

                i = 10;

                Console.WriteLine(i);

            }

           

            static void Main(string[] args)
            {
                Program obj = new Program();
                Program.display();
                
                Console.Read();
            }
        }

    C# does not support static local variable.Static variables are accessed with the name of the class, they do not require any object for access.

    If you can facing any c sharp concept that you are looking for some more, please leave a comment & Email.