Using Read-only versus const
There are Two difference versions of constants in C#: the Const keyword is used for compile –time constants read only keyword is used for runtime constants. The main issue with const is that it's evaluated at compile-time, which means if it's changed at a later date; you have to not only recompile that application, but all applications that reference that application. In contrast, read -only is evaluated at run-time providing you a lot more flexibility.
An overview of differences between const and read-only in c#
Const | Read only |
Cont be static | Can be either instance-level or static |
Value is evaluated at compile time | The value is evaluated at run time. |
It is initialized at declaration only | It can be initialized in declaration or by code in the code constructor .therefore read only fields have difference values depending on constructor used. |
So, const should really only be used for logical, real-world constants such as days of week, numeric Constants, etc. as for as performance goes, you actively loose the a little performance using read only, but you make up for it for it in flexibility of your application.
Comments