Enum in C#

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

  • Readability- Enums can make your code more readable by giving descriptive names to otherwise cryptic values. For example, instead of using the number 1 to represent Monday, you can use the enum value "Monday" to make the code more self-explanatory.
  • Type safety- Enums provide type safety by restricting the possible values that a variable can take on. This can help prevent errors caused by typos or incorrect values.
  • Maintainability- Enums make changing the underlying values they represent easy without changing the code that uses them. For example, if you need to add a new day of the week, you can add a new enum value without changing the code that uses the existing values.
  • Code organization- Enums can help you organize your code by grouping related values together. For example, you might use an enum to represent the different types of UserRoles in a Project.
  • Avoiding magic numbers- Enums can be used to avoid "magic numbers" in code, which are hard-coded values that have meaning only to the programmer who wrote the code. This can make code more maintainable and easier to understand for other programmers who may need to work on the code in the future.

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#.