C# - Convert enum to string

In C#, an enum (short for enumeration) is a user-defined data type that consists of a set of named integer constants. Enumerations are often used to improve code readability and maintainability by providing a clear and meaningful way to represent a finite set of values. Enum to string conversion allows you to convert an enum value into its corresponding string representation.

Here's a step-by-step explanation of enum to string conversion in C# with a complete source code example and its output:

  1. Define an Enum:

    First, you need to define an enum with a set of named constants. For example:

    
    public enum DaysOfWeek
    {
        Sunday,
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday
    }
    
  2. Convert Enum to String:

    To convert an enum value to its string representation, you can use the Enum.ToString() method or the ToString() method on the enum value itself. For instance:

    
    DaysOfWeek today = DaysOfWeek.Wednesday;
    string todayAsString = today.ToString();
    
  3. Complete Source Code Example:

    Below is a complete C# source code example that demonstrates enum to string conversion:

    
    using System;
    
    public enum DaysOfWeek
    {
        Sunday,
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday
    }
    
    class Program
    {
        static void Main()
        {
            DaysOfWeek today = DaysOfWeek.Wednesday;
            string todayAsString = today.ToString();
    
            Console.WriteLine("Today is: " + todayAsString);
        }
    }
    	

Output:

When you run the above program, it will output:


Today is: Wednesday

In this example, we defined an enum called DaysOfWeek, assigned a value of DaysOfWeek.Wednesday to the today variable, and then converted it to a string using the ToString() method. The result is that it correctly prints "Wednesday" as the output.

This demonstrates how enum to string conversion works in C# by providing a human-readable string representation of an enum value, which can be useful for various purposes like displaying user-friendly messages or interacting with external systems that expect string values.

Points to Remember:
  1. Enum Definition

    Start by defining an enum with a set of named constants that represent the possible values for a specific attribute or property.

  2. Enum Type

    Enumerations belong to an integral data type, typically represented as an integer, and each named constant within an enumeration possesses an underlying integer value. By default, these integer values start at 0, unless explicitly defined otherwise.

  3. ToString() Method

    To convert an enum value to a string, you can use the ToString() method. This method returns the name of the enum constant as a string.

  4. Human-Readable Names

    Enum to string conversion is often used to provide human-readable names for enum values, making code more understandable.

  5. Example
    
    public enum DaysOfWeek
    {
        Sunday,
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday
    }
    
    DaysOfWeek today = DaysOfWeek.Wednesday;
    string todayAsString = today.ToString(); // Converts to "Wednesday"
                
  6. String Representation

    The string representation is case-sensitive and matches the exact name of the enum constant. In the example above, the string representation of DaysOfWeek.Wednesday is "Wednesday," not "wednesday."

  7. Implicit Conversion

    Enum to string conversion is an implicit operation, meaning you don't need to explicitly cast the enum value to a string.

  8. Output Usage

    Enum to string conversion is useful for various purposes, such as displaying enum values in user interfaces, generating user-friendly messages, or interacting with systems that expect string representations.

  9. Error Handling

    When converting an enum to a string, ensure that the enum value is valid to prevent exceptions. Invalid enum values can lead to unexpected behavior or exceptions.

  10. Custom Formatting

    You can customize the string representation of enum values by using attributes or implementing custom logic. This allows you to control how enum values are displayed as strings.

  11. Enum.Parse()

    If you need to convert a string back to an enum value, you can use the Enum.Parse() method. This method takes the enum type and a string and returns the corresponding enum value.

  12. Type Safety

    Enum to string conversion helps maintain type safety in your code by ensuring that enum values are used in a consistent and readable way.

  13. Enum Underlying Type

    Enums can have different underlying types (e.g., byte, short, int, etc.), which can affect the range of values they can hold.