What is the difference between const and readonly in C#?
C# is a multi-purpose programming language with different ways to declare a variable. The const and readonly keywords declare
Syntax
This is how we declare a const variable in C#:
const int sum = 0;
This is how we declare a readonly variable in C#:
public readonly int sum = 0;
The code snippets above show that both keywords have almost the same declaration syntax. We add the public keyword before the readonly keyword to make the sum variable accessible outside of a class, as the readonly variable's scope is class-
Let's take a look at some examples to better understand how these two keywords work.
The const keyword
Let's display a Hello World! message using the const variable:
class HelloWorld{static void Main(){const string msg = "Hello World!";System.Console.WriteLine(msg);}}
Explanation
Line 5: We declare a
constvariable namedmsg. We can directly declare aconstvariable in theMain()function.
Immutability
The example below represents immutable const variables:
class HelloWorld{static void Main(){const string msg = "Hello World!";System.Console.WriteLine(msg);//Cannot re-assign value to a const variablemsg="New World!";System.Console.WriteLine(msg);}}
Explanation
Line 8: The changes in the above code snippet reflect the immutable property of
constvariables. If we try to assign any value after the first assignment, it throws an error. Hence, we can't set theNew World!value to themsgvariable.
The readonly keyword
Let's display a Hello world message via the readonly variable:
class HelloWorld{public class Temp{public readonly string msg;public Temp(){msg = "Hello World";}}static void Main(){Temp obj = new Temp();System.Console.WriteLine(obj.msg);}}
Explanation
Line 4–9: As
readonlyvariables are class-based, direct declaration in theMain()process is not possible. So, we design a dummy class namedTempthat will have ourHello Worldmessage.Line 12–13: We define an object of the
Temptype namedobj. The default constructor runs as soon asobjis declared, initializes thereadonlymsgvariable withHello World. We then access and print it in theMain()function using the access (dot) operator.member Class variables/attributes
Immutability
The example below represent the immutability of the readonly variable:
class HelloWorld{public class Temp{public readonly string msg;public Temp(){msg = "Hello World";msg = "New World!";}}static void Main(){Temp obj = new Temp();System.Console.WriteLine(obj.msg);//Uncomment line 17 to throw an error//as the readonly variable's value can only be changed// in the constructors.//obj.msg = "New World!";}}
Explanation
Line 6–7: We can change the value of the
readonlyvariable after the first initialization in the constructor.Line 17: The
readonlyvariable is immutable outside the scope of the class constructor.
Differences
These are the significant differences between the two keywords in C#:
Const | Readonly |
Scope can be both class-based or function-based. | Scope is class-based only. |
Value of the variable is known latest by the compile time. | Value of the variable is known latest by the runtime. |
It has no dynamic allocation of memory. | It has dynamic allocation of memory at runtime. |
Value cannot be changed after first initialization. | Value can only be changed in the constructors. |
Conclusion
The two keywords are similar, but differentiated by the way memory is allocated to their respective variables, and their scope in the program.
Free Resources