1Why Do We Need Readonly and Const?
Hi friends! Today we will learn the difference between readonly and const in C#. Both are used to create values that cannot be changed after a certain point. But they work differently.
const is used for values known at compile time. readonly is used when a value can be assigned at declaration or during object construction. We commonly use them to protect important application settings, configuration values and business rules.
2Think About a Bank Account
Imagine you are developing a banking application.
The minimum account balance requirement is ₹1,000. This value is fixed and does not change while the application runs. We can use const.
Now consider an account number. Every customer has a different account number. We receive it when creating the account. After that, it should not change. We can use readonly.
3Understand Their Main Differences
1. When Can We Assign Values?
A const value must be assigned when declaring it. A readonly field can be assigned during declaration or inside a constructor.
2. Compile Time vs Runtime
Compile time is when C# converts your code into executable instructions. Runtime is when your application actually executes. Const values are resolved at compile time, while readonly values can be determined at runtime.
3. Static Behaviour
Const fields are implicitly static, meaning they belong to the class rather than individual objects. Readonly fields can be instance fields or static fields.
4. Supported Values
Const supports compile-time constant values such as numbers, strings, booleans and enums. Readonly supports objects and values determined at runtime.
| Feature | const | readonly |
|---|---|---|
| Assignment | Declaration only | Declaration or constructor |
| Value timing | Compile time | Runtime possible |
| Static | Always | Optional |
| Object values | No | Yes |
| Modification | Not allowed | Not allowed after initialization |
4Let's Write a C# Program
Let's create a bank account class using both keywords. We will assign the account number through its constructor.
using System;
class BankAccount
{
public const double MinimumBalance = 1000;
public readonly string AccountNumber;
public BankAccount(string number)
{
AccountNumber = number;
}
public void Display()
{
Console.WriteLine("Account: " + AccountNumber);
Console.WriteLine("Minimum Balance: " + MinimumBalance);
}
}
class Program
{
static void Main()
{
BankAccount account =
new BankAccount("ACC1001");
account.Display();
}
}
Here, MinimumBalance is a constant value. AccountNumber receives its value through the constructor. We cannot assign a different value to AccountNumber after construction.
5Check the Program Result
Account: ACC1001 Minimum Balance: 1000
The program displays the account number and minimum balance. Both values are protected from reassignment according to their respective rules.
6Watch the Code Run in VS Code
Follow how the values are declared, initialized and displayed.
7What Should You Remember?
Both readonly and const help protect values from unwanted changes. Choose const when the value is known at compile time. Choose readonly when the value needs to be assigned during object creation or runtime initialization.
- const values must be initialized at declaration.
- readonly fields can be initialized in constructors.
- static readonly is useful for runtime class-level values.
8Questions Interviewers Love to Ask
What is the main difference between const and readonly?
Answer: Const values are fixed at compile time. Readonly fields can receive values during declaration or constructor execution.
Can we assign a readonly field inside a constructor?
Answer: Yes. A readonly field can be assigned inside the constructor of the class where it is declared.
Is const implicitly static in C#?
Answer: Yes. Const fields belong to the type and cannot be declared as instance constants.
What is static readonly?
Answer: It is a class-level field that can be initialized during declaration or inside a static constructor.
Can we modify the properties of an object stored in a readonly field?
Answer: Yes. Readonly prevents reassignment of the reference, not modification of the referenced object's mutable properties.