Monday, February 25, 2013

Constant key:
1.      Const is by default static but cannot mark as static.
For exp:   public static const int _strConst = 10; //error
                 //constant cannot mark static.
 public const int _strConst = 10; // good

    public class classConst
    {
        public const int _strConst = 1;

        static void Main(string[] args)
        {}
     }

2. Const key values must be initializing at the declaration time only.
3. Const key values is initializing at compile time.
4.  Const Key values cannot change at the Run time.
                       
Readonly key:
1.      In Readonly key marked as static.
            For exp:   public static readonly int _strReadOnly ;// good
            public readonly int _strReadOnly ;// good
            public readonly int _strReadOnly=10 ;// good

    public class ClassReadOnly
    {       
        public static readonly int _strReadOnly ;
        public ConstReadOnly()
        {
             _strReadOnly = 10;
        }
        static void Main(string[] args)
        {}
     }
2.      Readonly key is initializing maybe at the declaration time, in the constructor, in the web config.
3.      Readonly values is initializing at Run time.
4.      Readonly values can be changed at the Run time.

2 comments:

Anonymous said...

Hi Anil,
Good description of const Key and Read-only key,can you please give me real time use of these keys...

Anonymous said...

Hi Anil,
Good description of const Key and Read-only key,can you please give me real time use of these keys...