C# Enum TryParse() Method

The Enum.TryParse() method in C# safely attempts to convert the string representation of one or more enumerated constants to an equivalent enumerated object. Unlike Enum.Parse(), it returns a boolean value indicating success or failure instead of throwing an exception for invalid values.

Syntax

Following are the overloads for the Enum.TryParse() method −

public static bool TryParse<TEnum>(string value, out TEnum result)

public static bool TryParse<TEnum>(string value, bool ignoreCase, out TEnum result)

Parameters

  • value − The string representation of the enumeration name or underlying value to convert.

  • ignoreCase − A boolean value indicating whether to ignore case during the conversion.

  • result − When this method returns, contains an object of type TEnum whose value is represented by value if the parse operation succeeds.

Return Value

Returns true if the conversion succeeded; otherwise, false.

Using Enum.TryParse() with Case Sensitivity

Example

using System;

public class Demo {
    enum Vehicle { Bus = 2, Truck = 4, Car = 10 };
    
    public static void Main() {
        string[] VehicleList = { "2", "3", "4", "bus", "Truck", "CAR" };
        
        foreach (string val in VehicleList) {
            Vehicle vehicle;
            if (Enum.TryParse(val, true, out vehicle)) {
                if (Enum.IsDefined(typeof(Vehicle), vehicle) | vehicle.ToString().Contains(",")) {
                    Console.WriteLine("Converted '{0}' to {1}", val, vehicle.ToString());
                } else {
                    Console.WriteLine("{0} is not a value of the enum", val);
                }
            } else {
                Console.WriteLine("{0} is not a member of the enum", val);
            }
        }
    }
}

The output of the above code is −

Converted '2' to Bus
3 is not a value of the enum
Converted '4' to Truck
Converted 'bus' to Bus
Converted 'Truck' to Truck
Converted 'CAR' to Car

Using Enum.TryParse() with Validation

Example

using System;

public class ValidationDemo {
    enum Status { Active = 1, Inactive = 2, Pending = 3 };
    
    public static void Main() {
        string[] statusValues = { "Active", "invalid", "2", "pending", "0" };
        
        foreach (string val in statusValues) {
            Status status;
            if (Enum.TryParse<Status>(val, true, out status)) {
                if (Enum.IsDefined(typeof(Status), status)) {
                    Console.WriteLine("'{0}' is valid: {1}", val, status);
                } else {
                    Console.WriteLine("'{0}' parsed to {1} but is not defined", val, status);
                }
            } else {
                Console.WriteLine("'{0}' could not be parsed", val);
            }
        }
    }
}

The output of the above code is −

'Active' is valid: Active
'invalid' could not be parsed
'2' is valid: Inactive
'pending' is valid: Pending
'0' parsed to 0 but is not defined

How It Works

The TryParse() method works by attempting to match the input string against enum member names or their underlying numeric values. When ignoreCase is true, it performs case-insensitive comparison. The method uses the out parameter to return the parsed enum value and returns a boolean to indicate success.

Enum.TryParse() Process Flow Input String TryParse() Conversion Success: true Failure: false Enum Value Use Enum.IsDefined() to verify the parsed value exists in the enum

Common Use Cases

  • Configuration parsing − Converting string settings to enum values safely.

  • User input validation − Checking if user-provided strings are valid enum members.

  • API parameter handling − Converting string parameters to strongly-typed enum values.

  • Data import/export − Converting between string representations and enum values in data processing.

Conclusion

The Enum.TryParse() method provides a safe way to convert strings to enum values without throwing exceptions. It supports case-insensitive parsing and should be combined with Enum.IsDefined() to ensure the parsed value is actually defined in the enumeration.

Updated on: 2026-03-17T07:04:35+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements