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

By | November 6, 2017

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.

4 thoughts on “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. Fahad Bin Zia

    I like you post and improve my understanding as well.

  3. 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.

  4. Pingback: Dew Drop - November 7, 2017 (#2598) - Morning Dew

Leave a Reply

Your email address will not be published. Required fields are marked *

Verify Captcha * Time limit is exhausted. Please reload the CAPTCHA.