What are the differences between readonly and const?

readonly and const

The differences between readonly and const in C# lies in their usage, initialization, and behavior. Here’s a detailed explanation:

1. Initialization

const:

      • A const field must be initialized at the time of declaration.
      • Its value cannot change and is determined at compile time.
const int MaxUsers = 100; // Must be assigned at declaration.

readonly:

      • A readonly field can be initialized either at the time of declaration or in the constructor.
      • Its value is determined at runtime and can differ for each object of the class.
readonly int MinUsers; 
public MyClass(int min) 
{ 
    MinUsers = min; 

} // Can be set in the constructor.

2. Mutability

const:

      • Cannot be modified after its declaration.
      • It is immutable.

readonly:

      • Can only be assigned or modified in the constructor or variable initializer.
      • Once set, it cannot be changed outside the constructor.

3. Compile-Time vs Runtime

const:

      • The value of const is resolved at compile time (compile-time constant).
      • Only simple types (int, double, string, etc.) can be const.

readonly:

      • The value of readonly is determined at runtime, which allows flexibility.

4. Accessibility

const:

      • Implicitly static. It belongs to the type and not an instance.
      • Can be accessed using the class name.
Console.WriteLine(MyClass.MaxUsers); // No object needed.

readonly:

      • Not implicitly static. It belongs to an instance unless explicitly declared as static.
MyClass obj = new MyClass(10); Console.WriteLine(obj.MinUsers); // Requires an instance.

5. Use Case

const:

      • Use for values that are constant throughout the application, such as mathematical constants or configuration settings.
const double Pi = 3.14159;

readonly:

      • Use for values that need to be set once (at runtime) and never change afterward, like configuration values passed during object initialization.
readonly string ConnectionString; 
public MyClass(string connStr) 
{ 
    ConnectionString = connStr; 
}

Real-Life Example

public class ServerSettings {
    public const int MaxConnections = 100; // Fixed, compile-time constant.
    public readonly string ServerName; // Set during runtime.
    public ServerSettings(string serverName) 
    {
         ServerName = serverName; // Can differ per instance.
    }
}

// Usage
var server1 = new ServerSettings("ServerA");
var server2 = new ServerSettings("ServerB");

Console.WriteLine(ServerSettings.MaxConnections); // 100
Console.WriteLine(server1.ServerName); // "ServerA"
Console.WriteLine(server2.ServerName); // "ServerB"

Key Differences at a Glance

Feature const readonly
Initialization At declaration only Declaration or constructor
Value Determination Compile-time Runtime
Accessibility Implicitly static Instance-level by default
Mutability Immutable Can be set in constructor
Use Cases Fixed values (e.g., Pi) Instance-dependent (e.g., configuration)

 

Happy coding!

 

 


Mohammad Zubair

I'm Mohammad Zubair, a passionate software engineer working in the dynamic world of IT. Currently, I'm proud to be a part of HawarIT, a thriving Dutch-Bangladeshi joint venture company, where I contribute my expertise and enthusiasm to the field of software engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *