const and readonly are two very useful keywords in C# that as a .NET developer you might see in any real-world application. When used within the same class, these keywords often confuse us as developers. But, what are the similarities and differences between const and readonly?
Declaring CONST vs READONLY
Per definition, Constants are immutable values that are known at compile-time and do not change for the life of the program. An example of a const (Constant) declaration looks like below:
public const int NrOfMonths = 12;
On the other hand, a readonly variable is a runtime variable, so you do not need to assign a value at the time of declaration.
public readonly int Age;
It is really important to know that we can assign values to runtime variables at declaration time or through a non-static constructor.
public class ProgramUtilities { public const int NrOfMonths = 12; public readonly int Age; public ProgramUtilities(int age) { Age = age; } }
An example of these two variables within the same class looks like below:
In conclusion, const is a compile-time constant variable, which means you have to assign a value to a const variable when you declare it. Readonly is a runtime constant variable, which means you can assign values on runtime (after you have declared), or when you declare it.
Accessing READONLY and CONST variable values
Const is an implicit static variable, so you can access it using ClassName.Variable, here ProgramUtilities.NrOfMonth
Console.WriteLine($"NrOfMonths -> {ProgramUtilities.NrOfMonths}");
The complete example looks like below:
using System; namespace DotNetTips { public class Program { static void Main(string[] args) { Console.WriteLine($"NrOfMonths -> {ProgramUtilities.NrOfMonths}"); Console.ReadKey(); } } public class ProgramUtilities { public const int NrOfMonths = 12; public readonly int Age; public ProgramUtilities(int age) { Age = age; } } }
On the other hand, since the initialization of a readonly variable can be initialized either during the declaration of the variable or within the constructor of the same class, accessing these values is also done by using constructors. So, you need to create an object by passing as a parameter the readonly variable value and accessing it.
The complete example looks like below:
using System; namespace DotNetTips { public class Program { static void Main(string[] args) { Console.WriteLine($"NrOfMonths -> {ProgramUtilities.NrOfMonths}"); //Initialize ProgramUtilities object var programUtilities = new ProgramUtilities(7); Console.WriteLine($"NrOfDays -> {programUtilities.Age}"); Console.ReadKey(); } } public class ProgramUtilities { public const int NrOfMonths = 12; public readonly int Age; public ProgramUtilities(int age) { Age = age; } } }
When to use CONSTANTS vs READONLY?
In conclusion, use constants when the values are absolute, fixed, and there is no change, like DaysOfWeek, MonthsOfYears, mathematical PI value, etc. Use readonly when the variables come from user input, configuration files, or any other variable that do not have the same value, like a database connection string, a secret key, etc.