What are the differences between 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
constfield must be initialized at the time of declaration. - Its value cannot change and is determined at compile time.
- A
-
const int MaxUsers = 100; // Must be assigned at declaration.
readonly:
-
-
- A
readonlyfield 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.
- A
-
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
constis resolved at compile time (compile-time constant). - Only simple types (
int,double,string, etc.) can beconst.
- The value of
-
readonly:
-
-
- The value of
readonlyis determined at runtime, which allows flexibility.
- The value of
-
4. Accessibility
const:
-
-
- Implicitly
static. It belongs to the type and not an instance. - Can be accessed using the class name.
- Implicitly
-
Console.WriteLine(MyClass.MaxUsers); // No object needed.
readonly:
-
-
- Not implicitly
static. It belongs to an instance unless explicitly declared asstatic.
- Not implicitly
-
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!