C# Enum.Parse

Enum type

Enum.Parse converts strings to enum values. It is useful in programs that accept user input as a string, but store the value internally as an enum. An enum is a more efficient representation. Enum.TryParse too is covered.

Enum.Parse

Note

The Enum.Parse method is a static method in the System namespace, so you will need to include System at the top of your file or use the fully qualified name. The program shows an enum type containing a constant Dog with value of 2. When the string "Dog" is parsed with Enum.Parse, you get the PetType.Dog enum value. The tricky part is using typeof and casting.

Static Method
Program that parses enums [C#]

using System;

class Program
{
    enum PetType
    {
	None,
	Cat = 1,
	Dog = 2
    }

    static void Main()
    {
	// A.
	// Possible user input:
	string value = "Dog";

	// B.
	// Try to convert the string to an enum:
	PetType pet = (PetType)Enum.Parse(typeof(PetType), value);

	// C.
	// See if the conversion succeeded:
	if (pet == PetType.Dog)
	{
	    Console.WriteLine("Equals dog.");
	}
    }
}

Output

Equals dog.
Typeof operator

Description. In part A, the string here contains the value "Dog". In your program, it will likely be from user input or another source. In part B, it uses Enum.Parse. The typeof(PetType) part returns the type of the PetType enum declaration. The string is passed as the second argument. Finally, the result of Enum.Parse is casted to the PetType enum type. In part C, it tests the result. We see here that the Enum.Parse call was successful and the value is equal to PetType.Dog.

Typeof Operator

Exceptions

Programming tip

You can cause exceptions to be raised when parsing enums. When the contents of the string you try to parse is not represented in the enum, you must handle the exception. Handling exceptions is often one of the most important parts of your program.

Example program that shows enum exceptions [C#]

using System;

class Program
{
    enum PetType
    {
	None,
	Cat = 1,
	Dog = 2
    }

    static void Main()
    {
	// The enum doesn't contain this value.
	string value = "Bat";

	// 1.
	// 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;
	}

	// 2.
	// See if the conversion succeeded.
	if (pet == PetType.Dog)
	{
	}
    }
}

Output

FAILED
Requested value 'Bat' was not found.
Try keyword

Description. Please look at the try/catch block above. The example catches all errors, which is sufficient for many small applications. The string in the example isn't found in the enum PetType, so the enum variable is set to PetType.None. This is the fallback behavior. For important applications, you will log the exception.

Try Keyword Catch Examples

Enum.TryParse

Next let's examine the Enum.TryParse method. In this program please notice the names of the enumerated constants. Before calling Enum.TryParse, you can declare a variable of the enum type; you do not need to initialize it. Next, test the result of Enum.TryParse for true or false; if the method returns true, then the string was successfully parsed as an enum type.

Program that uses Enum.TryParse method [C#]

using System;

enum Importance
{
    None,
    Low,
    Medium,
    Critical
}

class Program
{
    static void Main()
    {
	// The input value.
	string value = "Medium";

	// An unitialized 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
.NET Framework information

Tip: Whenever you have a string that you want to convert into an enum, consider using the Enum.TryParse static method. This method provides a clear and useful calling convention, the tester-doer pattern, and enables this useful feature. This method requires the .NET Framework 4.0.

Summary

The C# programming language

We can use the Enum.Parse static method with the typeof operator result as the first parameter and the string input as the second parameter. We saw a successful call to Enum.Parse—and also one that failed and threw an exception. This method is useful for converting text or user input to internal enum values stored in memory and used throughout your application.

Enum Examples
.NET