Friday, 23 August 2013

Difference Between Const, ReadOnly and Static ReadOnly in C#


These are very common keywords and are quite confusing. So today we will discuss these keywords and 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.

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 the const variable, it gaves 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.



     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 do it gives me a compile time error as you can see in the snapshot above.

Now let's on to the readonly keyword.
 
2.     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:



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:




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.





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.



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.





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



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.
 
    2.    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.





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:




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

Const
Readonly
Static Readonly
mandatory to assign a value at Compile time.
Not mandatory to assign a value
Not mandatory to assign a value
By default a const is static and we cannot change the value of a const variable throughout the entire program
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.
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. 



0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (47) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (49) CD (1) CI (2) CloudComputing (2) Coding (8) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (19) Entity Framework (4) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (10) microservice (4) Mongodb (1) MVC (46) NodeJS (8) React (10) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (3) UI (1) UnitTest (2) WCF (14) Web Api (16) Web Service (1) XMl (1)

Dotnet Guru Archives