LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
C# Programming

Readonly vs Const in C#

Both are used to prevent a field's value from being changed, but they work differently. A const value is fixed during compilation. A readonly field can be assigned at declaration or during object construction. We commonly use them for fixed application values, configuration and settings.

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.

Remember const means fixed at compile time. Readonly means assignable during initialization, but not freely changeable afterwards.

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
Pro Tip Use static readonly when you need a class-level value that must be initialized at runtime.
Common Mistake A readonly reference prevents assigning another object to the field. It does not automatically make the referenced object's internal properties immutable.

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.

Program.cs
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

Output
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.

Program.cs - Visual Studio Code
● Program.cs   C#
◫⌕⑂⚙
1using System;
2
3class BankAccount
4{
5 public const double MinimumBalance = 1000;
6 public readonly string AccountNumber;
7
8 public BankAccount(string number)
9 {
10 AccountNumber = number;
11 }
12
13 public void Display()
14 {
15 Console.WriteLine("Account: " + AccountNumber);
16 Console.WriteLine("Minimum Balance: " + MinimumBalance);
17 }
18}
19
PROBLEMSOUTPUTDEBUG CONSOLETERMINAL
PS C:\Projects\Demo> dotnet run
Ready to execute...
C#UTF-8   CRLF   C#

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

Q1Easy

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.

Q2Easy

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.

Q3Medium

Is const implicitly static in C#?

Answer: Yes. Const fields belong to the type and cannot be declared as instance constants.

Q4Medium

What is static readonly?

Answer: It is a class-level field that can be initialized during declaration or inside a static constructor.

Q5Hard

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.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.