Yogendra Kumar

Yogendra Kumar

  • 692
  • 1.3k
  • 1.9m

Difference between Const, ReadOnly and Static ReadOnly?

Mar 25 2013 8:31 AM

Difference between Const, ReadOnly and Static ReadOnly?

    Hello Friend'z !!

   These are very common keywords and quite confusing. So today we will discuss on these keywords and try to understand.

1.       Const :- Const is nothing but Constant, a variable which value is constant but at compile time. And it's mandatory to assign it. By default Const is Static and we cannot change the value of Const variable throughout the whole Program.

 

 Here I have created a class named Variables and defined all three variables, so first let's play with Const.

 

Here I tried to de-initialize const variable, it's gave me an error like "A const field requires a value to be provided".  Ok now I initialize the value of this variable and try to change it further in class.

Here I have created static Ctor, Default Ctor, Parameterize Ctor and a Simple Method. I tried to change the value of const variable in everywhere but once I assign the value, I am not able to change it again. It's give me a compile time error as you can see in above snapshot.

Now let's move further to readonly Keyword.

 

2.       Readonly :- Readonly is the Keyword whose value we can change at Runtime or we can assign at run time but only through the non-Static Ctor. Not even method too. Let's see…

 

Here first I try to initialize value in static ctor. It gives me an error. Which you can see above.

Now I try to change the value in method, see what happened…

 

 

Here, it is also giving an error that you can only assign value either through variable or ctor.

now try to change value in default ctor.

 

 

Now in the above snapshot you can see its build successfully without any error, warning or messages. Let's check if there is Runtime error!! OK

 

Now here we can see that there is not any runtime error and value assigned successfully to Readonly variable.

Now one gotcha is this,

that you have assigned the value here and can you change this value again ???

Let's try to change the value again…..

 

 

Here I created  a parameterized ctor and created a new object, and passing a value as "Hello Frend'z" and as I build it gave me result "Build Succeeded".  Now let's move ahead and check for runtime error…..

 

 

See guys…. There is no runtime error !!  and value can be changed again and again through ctor.

 

Now move ahead for Static Readonly variable…

 

3.       Static ReadOnly :- Static Readonly type variable's value can be assign at runtime or assign at compile time and change at Runtime. But this variable's value can be only changed in static Ctor. And cannot change further. It can change only once at runtime. Let's understand it practically…

 

 

 Now above you can see that I took two variables one is not assigned and  one is assigned, and static Ctor.  Now in static Ctor  you can see that not assigned variable is getting assign and assigned value is being Changed. And there is not any compile time error. Further I try to again change this variable's value… see what happen 

 

 

As you can see above I created Default, Parameterize Ctor and Method and trying to change again value here. But I am getting compile time error for all….

 

Hope This article can help you…..

Thanks in advance…!!