Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C#
Tip/Trick

Const and Readonly Keywords

Rate me:
Please Sign up or sign in to vote.
4.44/5 (9 votes)
31 Jan 2013CPOL3 min read 59K   5   10
When, what, and how to use constants in C#.

Introduction

Both these words play a very important role in defining constants in an application (C#). At first sight, it seems like both are the same but that it is not he case. Let's understand them one by one to get a clear picture.

The word const itself means it will never change. If you are specifying any variable as a const, that means the value of the variable is never going to change inside the application.

How we declare a constant is, by using the const keyword. Basically, one can define const only on primitive types like int, double, etc. 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 const variable, that value will be set at the compile time itself and this value will get stored inside a .dll or a .exe. In the later part, I'll show you how we can see this value inside a DLL or an EXE using an Ildasm. Sample code to define a const variable is as:

Image 1

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. This means it is restricting us to a value assignment. Sample code to define readonly is as follows:

Image 2

Combined sample code with bit more depth

Please note, readonly variables can be assigned at the time of declaration or can be assigned 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.

Image 3

In the above code, let’s change the values of the PI and age variables inside the Main function as:

Image 4

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 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:

Image 5

When to use what?

If the value is going to fix throughout the program and is never going to change in any circumstances, then one should choose const.

But on the other hand, if assignment of initial value depends on some parameter/conditions and 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 till the lifetime of the application.

ILDASM and Constants

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

Image 6

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

Hope the above tip was useful.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionCode snippets still not in standard format Pin
CHill6017-Apr-14 2:13
mveCHill6017-Apr-14 2:13 
QuestionIncorrect information Pin
CHill601-Mar-14 2:17
mveCHill601-Mar-14 2:17 
AnswerRe: Incorrect information Pin
Shweta Lodha1-Mar-14 7:56
Shweta Lodha1-Mar-14 7:56 
AnswerRe: Incorrect information Pin
Giacomo Pozzoni29-Aug-14 3:02
Giacomo Pozzoni29-Aug-14 3:02 
I reported the same issue about static const 1 year earlier but it went quite unnoticed http://www.codeproject.com/Messages/4485312/static-const.aspx[^]

Glad to see more well trained C# developers around Smile | :)
QuestionWrong Information Pin
Aditya Thakur27-Feb-14 23:35
Aditya Thakur27-Feb-14 23:35 
AnswerRe: Wrong Information Pin
Shweta Lodha28-Feb-14 23:15
Shweta Lodha28-Feb-14 23:15 
GeneralMy vote of 2 Pin
♥…ЯҠ…♥13-Nov-13 21:31
professional♥…ЯҠ…♥13-Nov-13 21:31 
GeneralRe: My vote of 2 Pin
Shweta Lodha13-Nov-13 22:29
Shweta Lodha13-Nov-13 22:29 
GeneralMy vote of 1 Pin
fjdiewornncalwe31-Jan-13 15:27
professionalfjdiewornncalwe31-Jan-13 15:27 
Questionstatic const Pin
Giacomo Pozzoni31-Jan-13 8:13
Giacomo Pozzoni31-Jan-13 8:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.