What is the difference between readonly, const, and
static?
What is the difference between readonly, const, and
static?
Student
The Anubhav portal was launched in March 2015 at the behest of the Hon'ble Prime Minister for retiring government officials to leave a record of their experiences while in Govt service .
conststatic.Use when: Value is truly constant and known at compile time (like Pi, conversion factors, etc.)
readonlyUse when: Value should be set once at runtime (e.g., ID, creation timestamp).
staticUse when: You want a member that is shared across all instances (like configuration, counters, etc.)
Combined Usage
You can combine
staticwithreadonly:Means: shared across all instances, value set at runtime, but not modifiable afterward.
Summary Table
constreadonlystaticreadonly)