Sunday, 18 January 2015

Difference Between Constant, ReadOnly and Static variables

Constants

  • Constants variables must be initialized at the time of declaration
  • Constants variables cannot be modified at run time.
  • By default constant variables are static and we cannot define a constant variable as static.
  • Constant variables are evaluated at compile time.
  • We can use const keyword to built-in value types like byte, short, int, long, char, float, double, decimal, bool and reference type which can be assigned with a value null.
  • We can declare Constants as public, private, protected, internal, or protected internal access modifiers.
  • Constants must be used there where we know that the value of the constant would not be changed.

Example:
  
public void Calculation(int A)
    {
        const int X = 2, X1 = 70;
        const int Y = X + X1; //no error, evaluated a compile time
        const int Y1 = X + A; //gives error, evaluated at run time
    }

   const ClsName obj1 = null;//no error, evaluated a compile time
   const ClsName obj2 = new MyClass();//gives error, evaluated at run time


ReadOnly

  • Read only variable can be initialized at the time of declaration.
  • Read only variable can be modified at run time.
  • Read only variables are evaluated at run time.
  • Read only variables are by default not static but we can specify them as static.
  • Read only variable can be used with value type and reference type.Also used with the reference type which are initialized with new keyword.
  • Read only variable cannot used with delegate and events.
  • We can use the read only variable when we make a field constant at run time.

Example:

  class ClsName
    {
         readonly int Y = 20; // initialized at the time of declaration
         readonly int Y1;
    
       public ClsnNme(int x1)
         {
             Y1 = x1; // initialized at run time
         }
    }

Static
  • Static member is specified by the static keyword.
  • Static members are common to all objects of a class.
  • Static keyword can be used with classes, fields, methods, properties, operators, events, and constructors.
  • Static keyword cannot used with indexers, destructors and types.
  • If Static keyword is used with a class then all the members, properties and functions of the class must be static.
  • Static functions can only access static members of the same class.
  • Static properties of a class only get or set the value of static fields of the same class.
  • Static constructor cannot be parametrized. Access modifiers cannot be used with the static constructor, it is public be default.
Example:

     class ClsName
    {
        static int Y = 10;
        int Z = 20;

      public static void Show()
      {
        Console.WriteLine(Y);
        Console.WriteLine(Z); //error, since you can access only static members
      }
    }


1 comment:

  1. Hi Guys,

    If you have any questions regarding the blog please write to me.

    ReplyDelete