I have been getting options to declare the variables as const or readonly. But never cared about the basic difference of these two, except that one needs to be assigned a value at compile time and other at run time. But there is more to what this difference means between the two. Let’s try to evaluate that difference.
const : Values of a const are assigned at the compiled time itself and once assigned, cannot be changed. They are static in nature and we cannot use static keyword with them. They are also called as compile time constants, as their value must be set at the compile time itself.
readonly : Value of a readonly field can be set either when they are declared or can be assigned in the constructor of the class. They are also called as run time constants, as their values can be changed at run-time, but the point is that they can be only changed inside the constructor of the class.
As I mentioned that there is more to what these statements define here, next we will try to discuss that. Let’s start with an example. Suppose we have a class library, say SampleClass.cs, with two integer type variables a and b, declared in it. Here, a is of type const and b is of type readonly. Now we assign values to these variables as :
public const Int32 ConstInteger = 333;
public readonly Int32 ReadOnlyInteger = 666;
Next, we build the class library and add its dll in our client project and deploy the project on client machine. Now due to some requirement change, we need to change the value of ConstInteger to 444 and ReadOnlyInteger to 777. So we update the values of both the variables in the class library and replace the updated dll on the client’s machine. Then we check what the results are. A shocking one, the value for the ReadOnlyInteger changes but the ConstInteger still remains the same.
Why it happened ?
The reason is, that, for any const type variable being used in any another assembly, the actual code also has to be re-compiled, in order to update the value of the const in the Intermediate Language(IL) of the code where it is being used. But this is not the case with the readonly. This was the reason that the changes for readonly were reflected but not for the const type. It is like the value of the const was embedded in the IL of the code where we used the class library reference (when we compiled the code). So in order to change the value of the const, we need to re-compile the code where it is being used, to update its value in the IL.
Let’s try to check the behavior with a sample application. For this, we will create a class library having a class SampleClass.cs, with two integer variables, one of type const and other of type readonly. Then build the project.
Next we add a console application. Then, add reference to the SampleClass.dll in this application and simply print these values. So our code is like the following :
Finally, we generate a setup or .exe file for it and install the setup file on our system .Open the location where we installed it and run the application. We can see the values are printed.
Now, change the values of these variable to new values. Let’s say, change ConstInteger to 444 and ReadOnlyInteger to 777. This time, build the class library only and directly replace it’s dll on the location, where we installed the application. Now run the .exe application and see the results.
This time, value of only ReadOnlyInteger is changed. The same reason that we discussed above. So if we need to change the value of ConstInteger also, we need to re-build the console application also and then install it again. Try it and this time it will work.
The decision to choose between the two will matter the most in case we are providing service based dll’s to the clients. It becomes a dependency that the consumer client must re-compile the code in order to make sure that the updated value of the const is updated in his code also. So this is the behavior I was referring to when I said that there is more to what the definitions of these variables mean.
Hope you enjoyed reading it..!!!