C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program: Here we see an enum type "PetType" containing a constant "Dog" with value of 2.
Part 1: We call Enum.Parse. The typeof(PetType) returns the enum type. We cast the result of Enum.Parse to the PetType enum type.
Typeof, nameofPart 2: At the end, we test the result of the Enum.Parse method. We print a short message if the test succeeds.
C# program that parses enums
using System;
class Program
{
enum PetType
{
None,
Cat = 1,
Dog = 2
}
static void Main()
{
string value = "Dog";
// Part 1: try to convert the string to an enum.
PetType pet = (PetType)Enum.Parse(typeof(PetType), value);
// Part 2: see if the conversion succeeded.
if (pet == PetType.Dog)
{
Console.WriteLine("Equals dog.");
}
}
}
Output
Equals dog.
Example: Please look at the try-catch block. The code catches all errors, which is sufficient for many small applications.
String: The string in the example isn't found in the enum PetType, so the enum variable is set to PetType.None.
Note: This is the fallback behavior. For important applications, you will log the exception.
TryCatchC# program that shows enum exceptions
using System;
class Program
{
enum PetType
{
None,
Cat = 1,
Dog = 2
}
static void Main()
{
// The enum doesn't contain this value.
string value = "Bat";
// Try to convert the string to an enum.
PetType pet;
try
{
pet = (PetType)Enum.Parse(typeof(PetType), value);
}
catch (Exception ex)
{
// The conversion failed.
Console.WriteLine("FAILED");
Console.WriteLine(ex.Message);
// Set fallback value.
pet = PetType.None;
}
// See if the conversion succeeded.
if (pet == PetType.Dog)
{
}
}
}
Output
FAILED
Requested value 'Bat' was not found.
Then: Test the result of Enum.TryParse for true or false. If the method returns true, then the string was successfully parsed.
C# program that uses Enum.TryParse method
using System;
enum Importance
{
None,
Low,
Medium,
Critical
}
class Program
{
static void Main()
{
// The input value.
string value = "Medium";
// An uninitialized variable.
Importance importance;
// Call Enum.TryParse method.
if (Enum.TryParse(value, out importance))
{
// We now have an enum type.
Console.WriteLine(importance == Importance.Medium);
}
}
}
Output
True
Tip: Whenever you have a string that you want to convert into an enum, consider using the Enum.TryParse static method.
TryParse: This method provides a clear and useful calling convention, the tester-doer pattern. It requires the .NET Framework 4.0.
C# program that shows Enum.TryParse quirk
using System;
enum Importance
{
None = 0,
Low = 1
}
class Program
{
static void Main()
{
Importance importance;
// ... Try to parse the string "1".
if (Enum.TryParse("1", out importance))
{
// ... "1" is treated as "Low".
Console.WriteLine(importance);
}
}
}
Output
Low
Note: Thanks to Thorsten Pontow and Rob for providing information about how to use Enum.IsDefined with Enum.TryParse.
Quote: The example defines a Colors enumeration, calls the TryParse... method to convert strings to their corresponding enumeration values, and calls the IsDefined method.
Enum.TryParse: Microsoft DocsC# program that uses Enum.IsDefined
using System;
enum Importance
{
None = 0,
Low = 1
}
class Program
{
static void Main()
{
// ... "1" is not defined.
// ... "Low" is defined.
Console.WriteLine(Enum.IsDefined(typeof(Importance), "1"));
Console.WriteLine(Enum.IsDefined(typeof(Importance), "Low"));
}
}
Output
False
True