Convert string to Enum in C#

To convert a string to an enum in C#, you can use the Enum.Parse method. Here's an example:

string enumString = "Value1";
MyEnum myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), enumString);

This assumes that you have an enum called MyEnum with a value called Value1.

If you want to constrain a string to an enum (even for a generic type T), you can use the where keyword and the : Enum constraint. Here's an example:

public void DoSomething<T>(T value) where T : struct, Enum
{
    // method body
}

This method can now only be called with a value that is a struct and an enum.

To convert a List<string> to a List<MyEnum>, you can use LINQ to iterate through the list and parse each string into an enum. Here's an example:

List<string> stringList = new List<string> { "Value1", "Value2" };
List<MyEnum> enumList = stringList.Select(s => (MyEnum)Enum.Parse(typeof(MyEnum), s)).ToList();
  1. C# convert string to enum example: Converting a string to an enum in C#.

    string stringValue = "Monday";
    DaysOfWeek day = (DaysOfWeek)Enum.Parse(typeof(DaysOfWeek), stringValue);
    
  2. How to parse string to enum in C#: Parsing a string to an enum in C#.

    string stringValue = "Monday";
    DaysOfWeek day = (DaysOfWeek)Enum.Parse(typeof(DaysOfWeek), stringValue);
    
  3. String to enum conversion using Enum.Parse in C#: Using Enum.Parse for converting a string to an enum in C#.

    string stringValue = "Monday";
    DaysOfWeek day = (DaysOfWeek)Enum.Parse(typeof(DaysOfWeek), stringValue);
    
  4. Convert string value to enum in C#: Converting a string value to an enum using Enum.Parse.

    string statusString = "Active";
    Status status = (Status)Enum.Parse(typeof(Status), statusString);
    
  5. C# enum from string without case sensitivity: Converting a string to an enum without case sensitivity in C#.

    string stringValue = "monday";
    DaysOfWeek day = (DaysOfWeek)Enum.Parse(typeof(DaysOfWeek), stringValue, true);
    
  6. Using Enum.TryParse for string to enum conversion in C#: Using Enum.TryParse for safe string to enum conversion.

    string stringValue = "Monday";
    if (Enum.TryParse(stringValue, out DaysOfWeek day))
    {
        // Conversion successful
    }
    
  7. Handling invalid string values when converting to enum in C#: Handling invalid string values gracefully during string to enum conversion.

    string stringValue = "InvalidDay";
    if (Enum.TryParse(stringValue, out DaysOfWeek day))
    {
        // Conversion successful
    }
    else
    {
        // Handle invalid value
    }
    
  8. C# custom string to enum mapping: Creating a custom mapping function for string to enum conversion.

    string stringValue = "CustomValue";
    DaysOfWeek day = MapStringToEnum(stringValue);
    
    // ...
    
    private static DaysOfWeek MapStringToEnum(string value)
    {
        // Custom logic to map string to enum
        return (DaysOfWeek)Enum.Parse(typeof(DaysOfWeek), value, true);
    }
    
  9. Parsing enum from string with custom delimiter in C#: Parsing an enum from a string with a custom delimiter.

    string delimitedValue = "Red|Green|Blue";
    Colors color = (Colors)Enum.Parse(typeof(Colors), delimitedValue, true);
    
  10. C# enum conversion from string with default value: Converting a string to an enum with a default value in case of failure.

    string stringValue = "InvalidValue";
    DaysOfWeek day = Enum.TryParse(stringValue, out DaysOfWeek result) ? result : DaysOfWeek.DefaultValue;