Monday, August 6, 2012

Difference Between Readonly and Constant in C#

Read only variable can be assigned a value at the runtime. 
readonly variable can be assigned at run time which is must be in constructor. Readonly value can't be modified.



Where as const have to be assigned at the compile time only.
Constant variable must be assigned at the time of declaration only, which is can't be modified.

public class Const_Vs_Readonly
{
     public const int I_CONST_VALUE = 2;
     public readonly int I_RO_VALUE;
     public Const_Vs_Readonly()
     {
         I_RO_VALUE = 3;
     }
}

0 comments:

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Code Imagine