C# Constants Tutorial For Beginners With Examples

A C# constant is like a variable except that the value of a constant cannot be changed during the execution of the program.

Categories of Constants in C#

There are three different categories in which the constants are divided:
  • Literal Constants
  • Symbolic Constants
  • Enumerations
Literal Constants
A literal constant is a value. This value could be an integer, character, string, float etc.
There are following type of Literal constants in C#:
  • Boolean Literals
    The values true or false are Boolean literals.
  • Integer Literals
    The values which are of type int, uint, long and ulong are the Integer literals.
  • Real Literals
    The values which are of type float, double and decimal are Real literals.
  • Character Literals
    A Character literal is a single character enclosed in quotes. For example 'a', 'd' or 'h' are all Character literals.
  • String Literals
    The String literals are zero or more characters enclosed in double quotes. For example "Welcome" is a string literal.

    Sometimes a string literal is also prefixed with a @ symbol. For example @"Hello World". This type of string literals are called as verbatim string literals. This is mostly used to escape certain special characters.
  • Null Literal
    The null value is a Null literal.
Symbolic Constants
When a name is assigned to a constant value then it becomes a symbolic constant.
To declare a symbolic constant const keyword should be used along with the data type.
While declaring a symbolic constant it must be initialized.
In the below example, 28 is a Literal constant and day is a Symbolic constant of type int.
 const int day = 28;
        
Multiple symbolic constants of the same type can be declared and initialized in a single line.
 const int day = 28, year = 2, state = 3;
        
Enumerations
An enumeration consists of a set of named constants. This set of named constants is also called as enumerator list.
The enum keyword is used to create an enumeration. Enumeration is also known as Enums in C#.
Why Enumerations?
Enumeration helps in logical grouping of constants.
A similar set of constants which is not required to be changed during the execution of a program should be wrapped as an enumeration.
    enum WeekDay
    {
        Sunday = 1,
        Monday = 2,
        Tuesday = 3,
        Wednesday = 4,
        Thursday = 5,
        Friday = 6,
        Saturday = 7
    }
       
There is no need to explicitly assign values to an enumerated list. If no value is assigned then an enumerated list always starts with 0 and upwards.
    enum Color
    {
        Green,
        Blue
    }
       
In the above example the value of Green is 0 and Blue is 1.
Read more about enums in the enum tutorial.

Naming Conventions

Microsoft suggests the following naming conventions for constants:
  • For naming constants Pascal notation should be used. In Pascal notation the name should start with uppercase letter and if multiple words are there then they should also start with uppercase. Like WeekDay, MonthName, State etc.
  • Multiword names should not have any underscores. Constant names such as Month_Name is not as per Microsoft's standard.
  • Avoid special characters like backslash or hyphens in constants.