Const vs Readonly in C#: Key Differences and When to Use Each
const defines compile-time constants whose values cannot change and must be assigned at declaration, while readonly fields can be assigned once either at declaration or in a constructor and remain immutable afterward. const is static by default, but readonly can be instance-level or static.Quick Comparison
Here is a quick side-by-side comparison of const and readonly in C#.
| Factor | const | readonly |
|---|---|---|
| Assignment Time | At declaration only | At declaration or in constructor |
| Value Change | Never changes | Can be set once, then immutable |
| Storage | Implicitly static | Can be instance or static |
| Type Restrictions | Must be primitive or compile-time constant | Any type allowed |
| Performance | Replaced at compile time | Evaluated at runtime |
| Use Case | Fixed constants like Pi | Values set at runtime but constant after |
Key Differences
const fields are compile-time constants. This means their values are set when you write the code and cannot change later. They are implicitly static, so you access them through the class name, not an object instance. Because of this, const can only hold simple types like numbers, strings, or enums.
readonly fields are runtime constants. You can assign them when declaring or inside a constructor, allowing different instances to have different values. After assignment, their values cannot change. readonly fields can hold any type, including complex objects.
In summary, use const for fixed values known at compile time and readonly for values that are set once but only known at runtime.
Code Comparison
This example shows how to use const to define a fixed constant value.
public class Circle { public const double Pi = 3.14159; public double GetCircumference(double radius) { return 2 * Pi * radius; } } class Program { static void Main() { Circle c = new Circle(); System.Console.WriteLine(c.GetCircumference(5)); } }
Readonly Equivalent
This example shows how to use readonly to set a value once at runtime, here in the constructor.
public class Circle { public readonly double Pi; public Circle() { Pi = 3.14159; } public double GetCircumference(double radius) { return 2 * Pi * radius; } } class Program { static void Main() { Circle c = new Circle(); System.Console.WriteLine(c.GetCircumference(5)); } }
When to Use Which
Choose const when your value is truly constant and known at compile time, like mathematical constants or fixed configuration values. This makes your code faster and clearer.
Choose readonly when the value is set once but only known at runtime, such as values loaded from a file, database, or constructor parameters. This provides flexibility while ensuring immutability after initialization.
Key Takeaways
const is for compile-time fixed values and is implicitly static.readonly allows runtime assignment once, either at declaration or in constructors.const supports only simple types; readonly supports any type.const for fixed constants and readonly for runtime-set immutable values.readonly can be instance-level, while const is always static.