Thursday, 4 June 2015

MODIFIER CONST, READONLY,STATIC


-Const
Lets us look at example
Ex 1
namespace Sample                                                                         {
    public class MyClass
    {
        private const int a;

    }
    public class Sample2
    {
        static void Main()
        {       
        }
    }
}
// Outpur Error 1 A const field requires a value to be provided.

Const variable has to be initialized at the time of declaration and can’t be modified later.

Private static const int a=9; // Const can’t be marked as static

Ex 2
namespace Sample
{
    public class Sample1
    {
        private const Sample2 sample2 = new Sample2();//Error
      
    }
    public class Sample2
    {
   
    }
    public class Sample3
    {
        static void Main()
        {   
        }
    }
}
//Output  Error 1. A const field of a reference type other than string can only be initialized with null.

 A const field of a reference type other than string can only be initialized with null

Ex 3
namespace Sample
{
    public class Sample1
    {
        private const Sample2 sample2 = null;
      
    }
    public class Sample2
    {
   
    }
    public class Sample3
    {
        static void Main()
        { 
        }
    }
}

// Output No error

Ex4
namespace Sample
{
    public class Sample1
    {
        public const int a = 1;
        public const int b = b + 2;
    }
    public class Sample3
    {
        static void Main()
        {          
        }
    }
}
Value of b evaluates at compile time, A constant can participate in a constant expression.

-Static Field
Example 1
namespace Sample
{
    public class Sample1
    {
        public static int c1;
        static void Main()
        {
            Console.WriteLine(c1);
            Console.ReadKey();
        }
    }
}
//Output 0;
Static variable are initialized as soon as class load with the default value.                                            A variable in C# can never have an uninitialized value.
-ReadOnly
namespace Sample
{
    public class Sample1
    {
        public static readonly int a=8;
        static void Main()
        {
            a = 10;
            Console.WriteLine(a);
            Console.ReadKey();
        }
    }
}
//Output   Error 1 A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)
Readonly field can be declared either at time of declaration or  in constructor of same class.

Difference between const and Readonly keyword
A const field can only be initialized at the declaration of the field.                                                A readonly field can be initialized either at the declaration or in a constructor.     Therefore, readonly fields can have different values depending on the constructor used.            
Const field is a compile-time constant whereas Readonly field can be used for runtime constant.                                                                                                                                                
namespace Sample
{
    public class Sample1
    {
        public  readonly int _a; //but const field shows error if field not assigned
        public Sample1(int a)
        {
            _a = a;
        }
        static void Main()
        {
            Sample1 sample = new Sample1(5);
           
            Console.WriteLine(sample._a);
            Console.ReadKey();
        }
    }
}
//Output  5