Introduction

An enum, short for enumeration, is a value type in C# that allows you to define a set of named constant values. Enums are helpful when you have a fixed set of values that a variable can take, such as the days of the week or the colors. Enums can be defined using the enum keyword, followed by the name of the enum and a set of comma-separated constant values enclosed in braces.

Enums are typically used when you need to represent a small, finite set of values that are not likely to change, such as the days of the week or the HTTP status codes. Each constant value in an enum has an underlying numeric value, which defaults to 0 for the first constant and increments by 1 for each subsequent constant. You can also specify custom numeric values for each constant.

Example

// Days of Week
public enum DaysOfWeek
{
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
}
// Customer Types
public enum CustomerType
{
    Regular = 1,
    Premium = 2,
    VIP = 3
}

// Http Status Codes
public enum HttpStatusCodes
{
    Ok = 200,
    Created = 201,
    NoContent = 204,
    BadRequest = 400,
    Unauthorized = 401,
    Forbidden = 403,
    NotFound = 404,
    MethodNotAllowed = 405,
    RequestTimeout = 408,
    Conflict = 409,
    InternalServerError = 500,
    NotImplemented = 501,
    BadGateway = 502,
    ServiceUnavailable = 503,
    GatewayTimeout = 504
}

// Type of File Format
public enum FileTypes
{
    Pdf,
    Jpg,
    Png,
    Gif,
    Docx,
    Xlsx,
    Csv,
    Txt
}

// Types of User Roles
public enum UserRoles
{
    Guest,
    User,
    Moderator,
    Admin,
    SuperAdmin
}

Example- Enum is to check whether a given day is a weekday or a weekend.

using System;

public enum DaysOfWeek
{
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
}

class Program
{
    static void Main(string[] args)
    {
        DaysOfWeek today = DaysOfWeek.Sunday;

        if (today == DaysOfWeek.Saturday || today == DaysOfWeek.Sunday)
        {
            Console.WriteLine("It's the weekend!");
        }
        else
        {
            Console.WriteLine("It's a weekday.");
        }

        Console.ReadLine();
    }
}

// Output : It's the weekend!


using System;

public enum DaysOfWeek
{
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
}

class Program
{
    static void Main(string[] args)
    {
        DaysOfWeek today = DaysOfWeek.Monday;

        if (today == DaysOfWeek.Saturday || today == DaysOfWeek.Sunday)
        {
            Console.WriteLine("It's the weekend!");
        }
        else
        {
            Console.WriteLine("It's a weekday.");
        }

        Console.ReadLine();
    }
}

// Output : It's a weekday.

Advantages of Using Enum in Project

Summary

In general, enums are useful when you need to represent a fixed set of values in your code. They can improve readability, maintainability, and type safety, making your code easier to understand and less error-prone. They provide a simple and effective way to define related constant values in C# programming.

Thank you for reading, and I hope this blog post has helped provide you with a better understanding of the Enum in C#.