Mohanapriya R Mohanapriya R
Updated date Sep 06, 2024
In this blog, we will learn how to convert Enums to Strings in C#.
  • 1.4k
  • 0
  • 0

Convert Enums to Strings in C#

Enums are user-defined data types that consist of a set of named integral constants. They are valuable in scenarios where a variable can only take one of a predefined set of values, improving code readability and maintainability.

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

In the above example, we've defined an Enum named Days with distinct values representing the days of the week.

Converting Enums to Strings

To convert Enums to Strings in C#, developers often use the ToString() method, which is inherent to all Enum types. 

using System;

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

    static void Main()
    {
        // Converting Enum to String
        Days today = Days.Wednesday;
        string dayString = today.ToString();

        // Output
        Console.WriteLine($"Today is: {dayString}");
    }
}

In this program, we define an Enum Days and initialize a variable today with the value Days.Wednesday. The ToString() method is then used to convert the Enum value to a string, which is stored in the variable dayString. The output will be:

Today is: Wednesday

Handling Enum to String Conversion with Switch Statements

While the ToString() method is straightforward, handling Enum to String conversion with a switch statement provides greater control over the output:

using System;

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

    static void Main()
    {
        // Converting Enum to String with Switch Statement
        Days today = Days.Friday;
        string dayString = GetDayString(today);

        // Output
        Console.WriteLine($"Today is: {dayString}");
    }

    static string GetDayString(Days day)
    {
        switch (day)
        {
            case Days.Sunday:
                return "Sunday";
            case Days.Monday:
                return "Monday";
            case Days.Tuesday:
                return "Tuesday";
            case Days.Wednesday:
                return "Wednesday";
            case Days.Thursday:
                return "Thursday";
            case Days.Friday:
                return "Friday";
            case Days.Saturday:
                return "Saturday";
            default:
                throw new ArgumentOutOfRangeException();
        }
    }
}

In this example, we create a method GetDayString that takes a Days Enum parameter and uses a switch statement to return the corresponding string value. This approach allows for more customization and handling of edge cases.

Output:

Today is: Friday

Comments (0)

There are no comments. Be the first to comment!!!