Saturday, October 5, 2019

Difference between Constant and Read-Only

Constant

Constant is known as "const" keyword which is known at compile time. It is the constant variable which needs to be assigned at the time of declaring the variable. It's value cannot changed after that when it assigned.


ReadOnly

Readonly is known as "readonly" keyword which is known at compile time and run time. It is the constant variable which is initialized either can be declaration or at the constructor of the same class.


Comparison Chart

Constant Read-Only
Constant is used to declare fields or local variable. Read-Only is used to declare fields.
Constant can be computed at only compile time. Read-Only can be computed at compile as well as run time.
Constant can be assigned only at the time of declaration. Read-Only can be assigned either at the time of declaration or runtime when object is initialized.
Constant is implicitly static and the value cannot be changed through out the program. Read-Only can be changed at runtime only in non static constructor.

No comments:

Post a Comment