Loading...

Using Constants and Enums in C#

Learn how to use constants and enums in C# to write cleaner, safer, and more maintainable code with clear explanations and examples.

In programming, there are certain values that never change throughout the application. In such cases, constants (const) or enum structures are used. Constants are preferred to fix a value, while enums are used to define meaningful sets of constants. This makes the code more readable, more reliable, and easier to maintain.


Constants (const)

Variables defined with the const keyword cannot be changed during the program’s execution.


const double Pi = 3.14159;
const int MaxUser = 100;

Console.WriteLine("Value of Pi: " + Pi);
Console.WriteLine("Maximum users: " + MaxUser);

Here, Pi and MaxUser are constant values. They cannot be modified anywhere in the program.


Readonly Constants

Variables defined with readonly can only be assigned a value where they are declared or within a constructor. They cannot be changed afterward.


class Settings
{
    public readonly string Version = "1.0.0";

    public Settings(string version)
    {
        Version = version; // can be assigned inside the constructor
    }
}

Values defined with readonly can be determined at runtime, which makes them different from const.


Defining an Enum

An enum (enumeration) is a special data type that holds a group of related constant values. By default, it contains values of type int.


enum Days
{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}

class Program
{
    static void Main()
    {
        Days today = Days.Wednesday;
        Console.WriteLine("Today: " + today);
        Console.WriteLine("Numeric value of today: " + (int)today);
    }
}

In this example, an enum named Days is created and the value Wednesday is selected from it.


Enum Values and Switch Usage

Enum values are often used together with the switch statement.


Days today = Days.Friday;

switch (today)
{
    case Days.Monday:
        Console.WriteLine("Start of the week.");
        break;
    case Days.Friday:
        Console.WriteLine("Almost the weekend!");
        break;
    case Days.Sunday:
        Console.WriteLine("Weekend holiday.");
        break;
    default:
        Console.WriteLine("Normal day.");
        break;
}

Assigning Custom Values to an Enum

By default, enum values start from 0, but custom numbers can be assigned if desired.


enum ErrorCodes
{
    Success = 0,
    Warning = 100,
    Error = 200,
    Critical = 500
}

class Program
{
    static void Main()
    {
        ErrorCodes code = ErrorCodes.Error;
        Console.WriteLine($"Error Code: {(int)code} - {code}");
    }
}

This way, enums make constants such as error codes or status codes more meaningful.


TL;DR

  • const: Compile-time constant value, cannot be changed.
  • readonly: Can only be assigned where it is declared or in the constructor.
  • enum: Special type that groups meaningful constant values together.
  • Enum values can be easily used with the switch statement.