In this post we’ll learn about “What is the difference between const and readonly in C#?“. Here is another common and quite confusing question. What is read-only and constant variable? How do they differ? When & where 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 the difference is between these two.

To understand it better, first understand what a constant variable is. – 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.

01 What is the difference between const and readonly in C - https://codeconfig.in

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.

21 What is the difference between const and readonly in C - https://codeconfig.in

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.

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

41 What is the difference between const and readonly in C - https://codeconfig.in

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.

In above post you have learned about “What is the difference between const and readonly in C#?”. Hope this will help you in your daily development practices.

You can check more article on our website under C# section HERE or you can also check same on Micrsoft Docs too.

Leave a Reply

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

Explore More

Working With File Class In C#

In C#, ‘File’ class provides many static methods for moving, copying, and deleting files. These Static methods involve moving a file and copying and deleting a file also. Let’s check

How to Read a Text File in C#

In C# ‘File’ class provides static methods to read given text file data. The File.ReadAllText() method opens a text file, reads all the text present in the file into a

C# | How to use Interface references

In C#, you’re permitted to make a reference variable of an interface type or in other words, you’re permitted to form an interface reference variable. Such kind of variable can