Interview Question
Qus: Differentiate between constants and read-only variables.
Answers (2)
Evaluated at compile time
Support only value type variables
They are used when the value is not changing at compile time
Cannot be initialized at the time of declaration or in a constructor
Read-only Variables
Evaluated at run-time
They can hold the reference type variables
Used when the actual value is unknown before the run-time
Can be initialized at the time of declaration or in a constructor
2) const variables can declared in methods ,while readonly fields cannot be declared in methods.
3) const fields cannot be used with static modifier, while readonly fields can be used with static modifier.
4) A const field is a compile-time constant, the readonly field can be used for run time constants.