Variables

A variable is a name of the memory location which is used by the program to deal with the
logic in storing values of different data types.

There are different types of Variables and are classified as:

- Local variables

- Instance variables or Non – Static Variables

- Static Variables or Class Variables

- Constant Variables

- Readonly Variables

Local Variables:

A variable declared within a block or method or constructor is called a local variable.
These are also used in “for, switch, for each, catch and using statements”.

Instance variables or Non – Static Variables:

The instance variable is a variable defined in a class and the instantiation of the class is called
an instance variable or object of the class and each instantiated object has its own copy,
or instance.

 

using System;
namespace SampleProgram {
  class MySample // Class Declaration
  {
    int age;
    public static void Main(string[] args) //Main Method
    {
      MySample obj = new MySample(); //instance variable
      obj.age = 25;
      string name = “John”; // local variable
      Console.WriteLine("Name: " + name);
      Console.WriteLine("Age: " + obj.age);
    }
  }
}

 

The output will be:

Name: John
Age: 25

 

Static Variables or Class Variables:

Static variables are called as Class variables. The variable declared in a static block or variables with a static keyword is called Static
Variables.

Note: To access the static variables, we need to use the class name prefix instead of
the object of the class. So, objects are not required for static class or static variables.

ClassName.VariableName

 

using System;
namespace SampleProgram {
  class MySample // Class Declaration
  {
    static string name = “John”;
    static bool gender;
    public static void Main(string[] args) //Main Method
    {
      MySample.gender = “Male”;
      Console.WriteLine("MySample.name is " + MySample.gender);
    }
  }

 

The output will be:

John is Male.

 

Constant Variables:

Variables using the keyword “const” are Constant Variables. These variables should
need to assign value during the declaration time.
The values assigned to this variable are constant and cannot be changed.

Note: To access the constant variables, we need to use the class name prefix instead
of the object of the class.

 

using System;
namespace SampleProgram {
  class MySample // Class Declaration
  {
    const string name = “John”;
    static bool gender;
    public static void Main(string[] args) //Main Method
    {
      Console.WriteLine("Sample Name is " + MySample.name);
    }
  }
}

 

The output will be:

Sample Name is John

 

Readonly Variables:

Variables using the keyword “ReadOnly” are ReadOnly Variables.
These variables do not need to assign value during declaration but they cannot be
changed like constants once assigned a value.

 

using System;
namespace SampleProgram {
  class MySample // Class Declaration
  {
    const string name = “John”;
    readonly int age;
    public MySample() // Constructor
    {
      this.age = 30;
    }
    public static void Main(string[] args) //Main Method
    {
      MySample obj = new MySample();
      Console.WriteLine("Sample Name is " + MySample.name);
      Console.WriteLine("Age is " + obj.age);
    }
  }
}

 

The output will be:

Sample Name is John
Age is 30;

 

 

Related Tutorials