Const and Readonly Keywords in C#

Both of the words const and readonly play a very important role in defining constants in an application (C#). At one sight, it seems that they are the same but exactly it is not the case. Let's understand one by one to get the clear picture.

The word const itself means, it will never change. If you are specifying a variable as a const, in other words the value of the variable will never change inside the application.

How we declare a constant is, by using a const keyword. Basically, one can define const only on the primitive types, like int, double, and so on. One should make sure that the value should be assigned at the time of declaration itself and another important thing is whatever value is set for the const variable; that value will be set at the compile time itself and this value will be stored inside a DLL or an exe. In a later part, I'll show you how to see this value inside a DLL or an exe using Ildasm. Sample code to define a const variable is as:



Another keyword is readonly. The readonly word also sounds like a const but for the readonly variable you cannot change the value, once it is assigned. In other words it is not possible to assign a value. Sample code to define a readonly is as follows:



Please note, readonly variables can be assigned either at the time of declaration or can be assigned a value inside a constructor. These two are the only places where one can assign the value of a readonly variable. For readonly value assignment is done at run-time and there is no difference between a regular variable and a readonly variable in terms of memory allocation.

Combined sample code with a bit more depth



In the code above, let’s change the value of PI and the age variable inside the Main function as in the following:



Here one can notice that, reassigning both the variables named age and PI is giving an error and on mouse hover, we can see the complete description of the error as:



Now the question is, if both have the same qualities then what’s the point in creating two different things. Well, this is not the case because const is a compile time constant and readonly is a run-time constant. Most of us might be aware that the value of compile-time constants are set at the time of declaration itself and this can be seen in ildasm also. Coming to the run-time constants, these are set at run-time and that’s the reason that it is not mandatory to assign readonly variables at the time of declaration itself as one can assign them in a constructor also as:



Now question is when to use what

If the value will be fixed throughout the program and will never change in any circumstance then one should choose const.

But on the other hand, if the assignment of an initial value depends on some parameter/conditions and the value needs to be decided at run-time, then one can opt for readonly and based on that initial value of a readonly variable can be set. But please note, once the value is assigned, further modification is not at all possible for the lifetime of the application.

ILDASM and constants



Now, let's jump quickly to ildasm to prove the value assignment for both of these.

As I said earlier, const are compile time constants and are assigned at the time of declaration itself. So, that can be proved via ildasm using IL code. In ildasm, one can see the value of the const variable in hexadecimal but for readonly variables, there is no such value assigned to the PI variable in ildasm.


Similar Articles