Just like constants in mathematics, C# defines special fields of classes called constants. Like their name hints, once declared and initialized, constants maintain their values, forbidding their further modification.
There are two types of constants:
- constants for which the value is set during the compilation (compile time constants)
- constants which have their value set during the execution (run-time constants)
Compile time constants are declared using the C# modifier const:
1 |
[<access_modifiers>] const <type> <name>; |
A secret not many C# programmers know is that compile time constants are static fields, even if they do not contain the static keyword, and the compiler forbids its usage in the declaration.… Read more