Compare const and readonly in C# with examples and usage
This tutorial explains the const
and readonly
keywords for variables, highlighting their differences in terms of definition, usage, mutability, data types, and accessibility.
Const fields in C#
Definition
:
const
is declared for member variables only. It remains constant in definition, and once a value is assigned, it cannot be changed. This implies that the value is initialized at compile time, and reassignment is not possible at runtime.
A variable is declared with the const
keyword and initialized with a value.
const int PRICE = 100;
Console.WriteLine(PRICE);
- Data Types:
These are used for any value types that are initialized at compile time. This category includes primitive
types (int, float, string, and char) as well as custom
types, and Enums
.
They are not reassignable for complex data types and runtime expressions that are not resolved at compile time.
- Mutability:
Constants are immutable
, and reassigning with a new value is not possible.
An error is thrown if a new value is assigned to it.
const int PRICE = 100;
PRICE=200;
Console.WriteLine(PRICE);
A constant named PRICE
is declared and initialized with the value 100
.
An error(Compilation error (line 8, col 4): The left-hand side of an assignment must be a variable, property, or indexer) occurs at compile time if a new value is assigned to it.
Accessiblity:
- Constants are
immutable
andstatic
; they exist in the scope of atype
, not in an object. - Constants are accessed using the class name instead of the instance name.
- All instances of a class share the same constant value.
- Constants are
Usage:
The const
keyword is used in the following cases:
- Use const for variables if you know the value at compile time and the value is not reassignable.
- It is used for defining constant values, such as mathematical constants like PI, e, etc., configuration settings, and database connection strings that do not change their values.
readonly variable in C#
Definition:
variables can be declared with the readonly keyword at compile with an optional value, the values of these variables can be changed at runtime using an object (getter) or constructor.
Values can be assigned at declaration or in the constructor.
public class Employee
{
// Readonly variable
public readonly int Id;
public Employee(int id)
{
Id = id;
}
}
// Create instances of the class
Employee emp1 = new Employee(1);
Employee emp2 = new Employee(2);
- Mutability:
Runtime variable values can be changed at runtime using the constructor. Once assigned, it is not possible to reassign it with a new value. The value is decided at runtime, such as in the constructor expressions or getter. Once assigned, it becomes constant and provides immutability.
Accessibility:
- Readonly variables are non-static; they exist within the scope of an object.
- They are accessed using the object, not class names, unlike constants.
- All instances of a class share different readonly values.
Usage:
Readonly variables are used in the following cases.
When the variable value is not known until runtime execution. Once assigned the value the first time, it becomes constant and is not reassigned afterward.
- Data Types:
Readonly variables can be used with any data type, such as primitives and custom user-defined and complex objects. They allow the use of expressions whose values are known at runtime.
When to choose const and readonly
Both const
and readonly
provide immutability by preventing the assignment of new values.
- const is a compile-time constant.
- readonly is a runtime constant, assignable with a value the first time, but not assignable from the second time onwards.
readonly allows a little flexibility to assign values at runtime, which can be useful.
The decision to choose one over the other depends on whether the value is assigned at compile-time or runtime.
Difference between Const and readonly
Parameter | Description |
---|---|
Compile time constants | Runtime constants |
Value is initialized during compile time | Value can be assigned at runtime using object getter, constructor, or dynamic expression |
Variables declared with that are assigned at compile time | declared with types are resolved at Runtime time |
Static variables | Non-static for each instance |
accessed using class name | using instance name |
These distinctions help in understanding when to use const and when to use readonly based on the nature of constant values and their assignment timings.