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 immutableWhose value cannot be changed after the first assignment variables in C#. One example of the need for immutable variables is an interest calculator program that has a constant interest rate.

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;
Syntax of the readonly keyword

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-Blueprint for creating objects in the object-oriented programmingbased.

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 const variable named msg. We can directly declare a const variable in the Main() 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 variable
msg="New World!";
System.Console.WriteLine(msg);
}
}

Explanation

  • Line 8: The changes in the above code snippet reflect the immutable property of const variables. If we try to assign any value after the first assignment, it throws an error. Hence, we can't set the New World! value to the msg variable.

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 readonly variables are class-based, direct declaration in the Main() process is not possible. So, we design a dummy class named Temp that will have our Hello World message.

  • Line 12–13: We define an object of the Temp type named obj. The default constructor runs as soon as obj is declared, initializes the readonly msg variable with Hello World. We then access and print it in the Main() function using the memberClass variables/attributes access (dot) operator.

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 readonly variable after the first initialization in the constructor.

  • Line 17: The readonly variable 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

Copyright ©2025 Educative, Inc. All rights reserved