Back to Basics – What is the difference between const and readonly in C#?

Back to Basics – What is the difference between const and readonly in C#?

Continuing with the Back to Basics series, here is another common and quite confusing question. What is read-only and constant variable? How do they differ? When should we use constant over Read-only? const and readonly, are very common keywords and are quite confusing when you placed them with each other. Let’s try to get into it and understand what is the difference.


To understand it better, first understand what is a constant variable? – The variable whose value can’t be changed during the entire execution of the program. We define the constants variable using const and readonly keyword as shown in the below snippet.

// Defining Constant
public const int maxLength= 100;

// Defining Read Only
public readonly double ratio = 1.4;

As said, with the above snippet,  both the keywords are doing exactly the same thing. If we declare a member variable with const or readonly we can’t change it further, which means the values of maxLength and ratio would remain unchanged. In case you want to assign any other values to these variables you will get an error.

So, what is the difference? The difference is when we assign the values to constant variables. You can say, Const is a compile-time constant variable, whereas, the readonly is a runtime constant variable.

To make it simpler, assignment of the const must be done during declaration itself. We can’t do it anywhere else.

public const int maxLength= 100;

When this assigned, this is fixed and can’t modified further while your application is running. You can check the compile time assignment in IL as well using ILDASM Tool.

 

Did you Know – You can launch ILDASM Tool from the Visual Studio itself – How ?

 

Here you can also see, Const is implicit static variable, so you can access it using ClassName.Variable, here Program.MaxLength
In the other hand, declaration and initialization of the values can be done in different places for read-only. It can be initialized either during the declaration of the variable or within the constructor of the same class.

// Declaration of Read Only
public readonly double ratio;

// Constructor
public Program()
{
// Initilze of Read-Only Variables
ratio = 1.4;
}

In case of read-only, if you want to assign a value from user input or from configuration files, you may do so within the constructor – during initialization.

Use constant when the values are absolute fixed and there is no changes, like maxlength, use read-only when the values of the variables comes from user input, or configuration files, or from another variable.

To Summarize it, const is the compile-time constants and has to be initialized during the time of declaration only and read-only are runtime constants for which initialization of readonly can be done during declaration as well within the public constructor of the same class.

Hope this helps.

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

4 Comments to “Back to Basics – What is the difference between const and readonly in C#?”

  1. JD

    There’s more to it. If you change a constant, all clients (i.e. users of your library) must recompile. This is not the case for static readonly variables.

  2. Ashwin

    Great post. Many developers aren’t aware about such basic differences and the back to basics series posts will truly help them a lot.

Comments are closed.