- Const can only be initialized at the time of declaration of the field.
- Const values will evaluate at compile time only.
- Const value can’t be changed these will be same at all the time.
- This type of fields are required when one of the field values remains constant throughout the system like Pi will remain same in your Maths Class.
- The value will be initialized either declaration time or the constructor of the class allowing you to pass the value at run time.
- Read only values will evaluate at runtime only.
public class Const_VS_Readonly
public const int I_CONST_VALUE = 2;
public readonly int I_RO_VALUE;
I_RO_VALUE = 3;
Suppose if you want the value of the constant won’t change use a const or if you have a constant that may change or when in doubt, use a readonly. I hope it helps.