C# Enum Tutorial: How to Use Enums, Examples, and Best Practices

C# Enum Tutorial: How to Use Enums, Examples, and Best Practices

An enum (enumeration) in C# is a special value type that defines a set of named constants.

Enums are used to represent a group of related constant values in a readable and meaningful way. Instead of using raw numbers or strings, enums allow developers to define named values, making the code easier to understand and maintain. Internally, each enum value is associated with an integer by default, starting from 0. Enums are commonly used for states, categories, options, and fixed sets of values.

When is Enum Needed?

Use enums when:

• You have a fixed set of related values
• You want to improve code readability
• You want to avoid magic numbers
• You are representing states (e.g., OrderStatus)
• You need type safety for predefined options

How to Use Enum in C#?

Basic Enum Example

using System;

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

    static void Main()
    {
        Days today = Days.Monday;

        Console.WriteLine(today);
    }
}

Enum with Switch Statement

using System;

class Program
{
    enum OrderStatus
    {
        Pending,
        Processing,
        Shipped,
        Delivered
    }

    static void Main()
    {
        OrderStatus status = OrderStatus.Shipped;

        switch (status)
        {
            case OrderStatus.Pending:
                Console.WriteLine("Order is pending");
                break;

            case OrderStatus.Shipped:
                Console.WriteLine("Order has been shipped");
                break;

            default:
                Console.WriteLine("Other status");
                break;
        }
    }
}

Enum with Custom Values

using System;

class Program
{
    enum ErrorCode
    {
        None = 0,
        NotFound = 404,
        ServerError = 500
    }

    static void Main()
    {
        ErrorCode code = ErrorCode.NotFound;

        Console.WriteLine((int)code);
    }
}

Enum Overview Table

Feature Description
Type Value type
Default values Starts from 0
Storage Internally stored as integers
Purpose Named constant values

Advantages and Disadvantages

Advantages Disadvantages
Improves code readability Limited flexibility compared to classes
Prevents invalid values Requires updates if values change
Reduces magic numbers Cannot contain complex data
Strongly typed constants Needs casting for numeric operations

Similar and Alternative Options

Option Description When to Use
Constants Fixed values using const keyword Simple fixed values
Static Classes Group related constants More complex grouping
Flags Enum Bitwise enum operations Multiple selections
String Constants Named string values When readability is enough

Common Mistakes

• Using enums for dynamic or changing values
• Not assigning meaningful names to enum members
• Assuming enum values cannot be changed
• Forgetting to cast enum to int when needed
• Overusing enums instead of classes or databases
• Not handling invalid enum values in switch statements

Contents related to 'C# Enum Tutorial: How to Use Enums, Examples, and Best Practices'

C# String to Int Conversion Errors: Parsing, TryParse, and Best Practices
C# String to Int Conversion Errors: Parsing, TryParse, and Best Practices
C# var vs dynamic: Differences, Usage, and Best Practices
C# var vs dynamic: Differences, Usage, and Best Practices