C# Convert String to enum

  1. C# string to enum Conversion - Using Enum.Parse() and Enum.TryParse() Methods
  2. C# Program to Convert string into an enum with Exception Handling Using Enum.Parse() Method
  3. C# Program to Convert string into an enum Using Enum.TryParse()
  4. C# Program to Check String Value Existence Using Enum.IsDefined() Method
  5. Conclusion

In C# we use Enum.Parse() and Enum.TryParse() methods to convert a string into an enum. Here we have given examples for both, but we recommend you to use Enum.TryParse() method when converting a string into an enum.

C# string to enum Conversion - Using Enum.Parse() and Enum.TryParse() Methods

In order to convert a string into an enum, we can use Enum.Parse() method. You will have to carefully include System at the top of your file as Enum.Parse() is a static method.

Enum type, the string value and an indicator(optional) are the parameters of this method. The correct syntax to use Enum.Parse() and Enum.TryParse() is following:

EnumName VariableName= (EnumName)Enum.Parse(typeof(EnumName), StringValue);
EnumName VariableName= (EnumName)Enum.Parse(typeof(EnumName), StringValue,indicator); //indicator can be true or false
//To implement Enum.TryParse()
EnumName VariableName;
Enum.TryParse(StringValue, out VariableName);

Now we are going to implement this in a c# program.

C# Program to Convert string into an enum Using Enum.Parse()

using System;
class Conversion{
      enum Flowers
      {
         None,
         Daisy= 1,
         Lili = 2,
         Rose = 3
      }
      static void Main()
      {
         string stringvalue = "Rose";
         Flowers Flower = (Flowers)Enum.Parse(typeof(Flowers), stringvalue);
          //To check if we have converted successfully
         if(Flower == Flowers.Rose)
         {
            Console.WriteLine("The Flower is Rose");
         }
      }
}

Output:

The Flower is Rose

Here we have an enum named as Flowers containing multiple constants with their respective values. We have called Enum.Parse() method to convert the string value Rose into an enum. The typeof(Flowers) will return the enum type. In the end, we have checked if our conversion was successful or not.

Sometimes, an exception may occur if the string that we are trying to convert is not available in the enum. To avoid this we will use try-catch block.

C# Program to Convert string into an enum with Exception Handling Using Enum.Parse() Method

using System;

class Conversion{
      enum Flowers
      {
         None,
         Daisy= 1,
         Lili = 2,
         Rose = 3
      }
      static void Main()
      {
         stringvalue = "Jasmine";
         try
         {
         Flowers Flower = (Flowers)Enum.Parse(typeof(Flowers), stringvalue);
         }
         catch (Exception ex)
         {
         Console.WriteLine("Not Possible");
         //Set value to none
         Flower = Flowers.none;
         }
      }
}

Output:

Not Possible

C# Program to Convert string into an enum Using Enum.TryParse()

using System;
enum Flowers
      {
         None,
         Daisy= 1,
         Lili = 2,
         Rose = 3
      }
class Conversion
{
      static void Main()
      {
         string stringvalue = "Rose";
         //Using Enum.TryParse() 
         Flowers Flower;
         if(Enum.TryParse(stringvalue, out Flower))
         {
            Console.WriteLine(Flower == Flowers.Rose);
         }
      }
}

Output:

True

We can also pass the value of constant in enum as a parameter in Enum.TryParse() method. Following program implements this:

using System;

enum Flowers
      {
         None,
         Daisy= 1,
         Lili = 2,
         Rose = 3
      }
class Conversion
{
      static void Main()
      {
         Flowers Flower;
         
         if(Enum.TryParse("3", out Flower))
         {
            Console.WriteLine(Flower);
         }
      }
}

Output:

Rose

You can also check either the string value that you want to convert is defined in enum or not using Enum.IsDefined() method.

C# Program to Check String Value Existence Using Enum.IsDefined() Method

using System;

enum Flowers
      {
         None,
         Daisy= 1,
         Lili = 2,
         Rose = 3
      }
      
class Conversion{
      
      static void Main()
      {
         Console.WriteLine(Enum.IsDefined(typeof(Flowers), "Rose"));
      }
}

Output:

True

Conclusion

Enum.Parse() and Enum.TryParse() are the methods that are used to convert a string into an enum. But it is the best practice to use Enum.TryParse().

Related Article - Csharp String

  • C# Convert a char to an int
  • C# Convert String to datetime
  • Related Article - Csharp Enum

  • C# Convert String to datetime
  • How to Convert String to Int in C#
  • comments powered by Disqus