Definition - What does Constant mean?
In the context of C#, a constant is a type of field or local variable whose value is set at compile time and can never be changed at run time. It is similar to a variable by having a name, a value, and a memory location. However, it differs from the variable by its characteristic of getting initialized only once in the application. A constant is declared using the keyword "const".
Variables of built-in type, excluding System.Object whose non-changing value is known at compile time, can be declared as constants. During compilation, the constant value is substituted for its literal value into the intermediate language code by the compiler. This results in better integrity of the application by reducing accidental bugs. Usage of constants also improves performance over regular variables. It improves code readability and provides better maintenance, since it is easier to update the constant value in a single place before recompilation of software.
Techopedia explains Constant
A constant has the following characteristics or must adhere to the following rules:
- One or more constants of a given type can be declared in a single declaration.
- A constant can be of any type, such as an sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, string, enum-type, or reference type.
- A type of a constant should be at least as accessible as the constant itself.
- A constant can depend on other constants if dependencies are not of a circular nature.
- A constant cannot be used for a method, property, or event.
- A constant cannot be of user-defined types, such as class, struct, and array.
- While declaring a constant, a static modifier cannot be used.
- Assigning an already assigned constant variable at runtime results in a compilation error.
- As no address is associated with a constant at runtime, it cannot be passed by reference and cannot appear as an l-value in an expression.
- A constant can be used with access modifiers such as public, private, protected, internal, or protected internal.
- Constant declarations made at the class level are stored in assembly metadata.
Some of the best practices while using constants include:
- Constants need to be initialized during declaration.
- Constants are to be used with meaningful names as they represent special values.
- To define multiple non-integral/integral constants, a single static class (containing constant member variables) can be used to group them.
- The scope of the constant variable is limited to a single assembly, class, or method. Hence, while referring constant values defined in other assemblies, it has to be recompiled for any modification before compiling the dependent assembly.
A constant differs from the readonly variable in that the former needs to be initialized during declaration time and is static, while the latter can be initialized during declaration or any one time (like in a constructor so as to have different values based on the constructor type). Hence, a constant is called a compile-time constant, and a readonly variable is a runtime constant.