TechieClues . TechieClues .
Updated date Aug 21, 2022
This blog will teach us how to convert a string to an enum using C#.

In this blog, we will learn how to convert a string to an enum using C#.

We will use the below 2 methods in C# to convert a string to an enum.

  • Enum.TryParse() - It converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. It also returns a value that indicates whether the conversion succeeded.
  • Enum.Parse() - It converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

Convert a String to an Enum Using Enum.TryParse() Method:

We will see below the overload methods for Enum.TryParse() method,

  • TryParse(Type, ReadOnlySpan<Char>, Object)    
  • TryParse(Type, String, Object)    
  • TryParse(Type, ReadOnlySpan<Char>, Boolean, Object)    
  • TryParse(Type, String, Boolean, Object)    
  • TryParse<TEnum>(ReadOnlySpan<Char>, TEnum)    
  • TryParse<TEnum>(String, TEnum)    
  • TryParse<TEnum>(String, Boolean, TEnum)    
  • TryParse<TEnum>(ReadOnlySpan<Char>, Boolean, TEnum)

The Enum.TryParse() method is used to convert a string to an enum. It returns true if the conversion succeeded; otherwise, returns false

 The below example shows how to convert a string to an enum using Enum.TryParse() method,

using System;
namespace StreamWriterSample
{
    class Program
    {
        // Define Enum
        enum Colours
        {
            Red = 1,
            Blue = 2,
            Green = 3,
            Pink = 4 ,
            White = 5
        }
        static void Main(string[] args)
        {
            Colours colors;
            var color1 = "Blue";
            var color2 = "Green";
            var color3 = "Yellow";           
            
            if (Enum.TryParse<Colours>(color1, out colors))
                Console.WriteLine(color1);

            if (Enum.TryParse<Colours>(color2, out colors)) 
                Console.WriteLine(color2);

            if (Enum.TryParse<Colours>(color3, out colors))
                Console.WriteLine(color3);
            else
                Console.WriteLine("Conversion Faild: {0}", color3);
            Console.ReadKey();
        }
    }
}

Output:

Blue
Green
Conversion Faild: Yellow

Convert a String to an Enum Using Enum.Parse() Method:

The below example shows how to convert a string to an enum using Enum.Parse() method,

using System;
namespace StreamWriterSample
{
    class Program
    {
        // Define Enum
        enum Colours
        {
            Red = 1,
            Blue = 2,
            Green = 3,
            Pink = 4 ,
            White = 5
        }
        static void Main(string[] args)
        {
            Colours colors;
            var color1 = "BLUE";
            var color2 = "Green";
            var color3 = "Yellow";

            Colours weekDay1 = (Colours)Enum.Parse(typeof(Colours), color1, true); // This will ignore the case to compare the string
            Colours weekDay2 = (Colours)Enum.Parse(typeof(Colours), color2);

            Console.WriteLine(weekDay1);
            Console.WriteLine(weekDay2);
            
            // The color Yellow is not present in the Enum so It will return an error.
            try
            {
                Colours weekDay3 = (Colours)Enum.Parse(typeof(Colours), color3);
                Console.WriteLine(weekDay3);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error Occured: "+ ex.Message);
            }
            Console.ReadKey();
        }
    }
}

Output:

Blue
Green
Error Occured: Requested value 'Yellow' was not found.

 

ABOUT THE AUTHOR

TechieClues .
TechieClues .
Software Professional, India

IT professional with 15+ years of experience in Microsoft Technologies with a strong base in Microsoft .NET (C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices,...Read More

https://www.techieclues.com/profile/alagu-mano-sabari-m

Comments (0)

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