These are very common keywords and are quite confusing. So today we will discuss these keywords and try to understand them.
- 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 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.
Here I have created a static constructor, a default constructor, a 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 read-only keyword. - 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 created a new object, and passed 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 !! The value can be changed again and again through a constructor.
Now move ahead to Static Readonly variables. - 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. Furthe,r 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.
EmoticonEmoticon