Difference between const and readonly:
The primary difference between read-only
and const
keywords is that const
represents a compile-time constant, whereas read-only
is a runtime constant.
The Constant variables must be initialized at compile-time with fixed values. Their values are known during compilation and, once assigned, cannot be changed afterwards.
On the other hand, Read-Only variables are also immutable. They can be assigned a value either at the time of declaration or at runtime within the constructor but cannot be modified afterwards for the life of the program.
![difference between readonly and const keyword in c#](https://www.shekhali.com/wp-content/uploads/2023/02/difference-between-readonly-and-const-keyword-in-csharp-1024x417.png)