Monday, August 4, 2014

Difference Between Const, ReadOnly and Static ReadOnly in C#

C#

This article is more about to understand most frequently used keywords in daily use.So we will try to understand them.

  1. Const: Const is nothing but "constant", a variable of which the value is constant but at compile time. And it's mandatory to assign a value to it. By default a const is static and we cannot change the value of a const variable throughout the entire program.


image

Here I have created a class named Variables and defined all three variables, so first we’ll look into const.


image


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


image

 


Here I have created a static constructor, default constructor, parameterized constructor and a Simple Method. I tried to change the value of the const variable everywhere but once I assign the value, I am unable to change it again since when I try to change its value ,it prompts me an error as you have seen above.

Now let's on to the Readonly keyword.

 

  1. Readonly: Readonly is the keyword whose value we can change during runtime or we can assign it at run time but only through the non-static constructor. Not even a method. Let's see:

  2. image


Here first I try to initialize the value in the static constructor. It gives me an error. Which you can see above.
Now I try to change the value in a method, see what happened:


image


Here, it is also giving an error that you can only assign a value either through a variable or a constructor.
Now try to change the value in the default constructor.


image


Now in the snapshot above you can see it's built successfully without an error, warning or messages. Let's check if there is a runtime error. OK.


image


Now here we can see that there is not a runtime error and the value was assigned successfully to the Readonly variable.
Now one gotcha is, now that you have assigned the value, can you change this value again ???


Let's try to change the value again.


image


Here I created  a parameterized constructor and created a new object, and passing a value as "Hello Friend's" and as I built it, it gave me the result "Build Succeeded".  Now let's move ahead and check for a runtime error:


image

See guys. There is no runtime error !!  And the value can be changed again and again through a constructor.
Now move ahead to Static Readonly variables.

  1. Static ReadOnly: A Static Readonly type variable's value can be assigned at runtime or assigned at compile time and changed at runtime. But this variable's value can only be changed in the static constructor. And cannot be changed further. It can change only once at runtime. Let's understand it practically.

image

Now in the preceding you can see that I used two variables, one is not assigned and another is assigned, and the static constructor.  Now in the static constructor you can see that the unassigned variable is being assigned and the assigned value is being changed. And there is no compile time error. Further I try to again change this variable's value.  See what happened:

image

As you can see in an image above, After creating Default , Parameterized Constructor and Method I tried to change the value again here. But getting a compile time error for all.

To know more about MVC please go through with given below link.

MVC Articles

ThanksSmile

Keep reading and Smile Smile

0 comments :

Post a Comment