Edit

Share via


Enum.Parse Method

Definition

Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

Overloads

Parse(Type, ReadOnlySpan<Char>)

Converts the span of characters representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

Parse(Type, String)

Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

Parse(Type, ReadOnlySpan<Char>, Boolean)

Converts the span of characters representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. A parameter specifies whether the operation is case-insensitive.

Parse(Type, String, Boolean)

Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. A parameter specifies whether the operation is case-insensitive.

Parse<TEnum>(String, Boolean)

Converts the string representation of the name or numeric value of one or more enumerated constants specified by TEnum to an equivalent enumerated object. A parameter specifies whether the operation is case-insensitive.

Parse<TEnum>(ReadOnlySpan<Char>, Boolean)

Converts the span of characters representation of the name or numeric value of one or more enumerated constants specified by TEnum to an equivalent enumerated object. A parameter specifies whether the operation is case-insensitive.

Parse<TEnum>(ReadOnlySpan<Char>)

Converts the span of characters representation of the name or numeric value of one or more enumerated constants specified by TEnum to an equivalent enumerated object.

Parse<TEnum>(String)

Converts the string representation of the name or numeric value of one or more enumerated constants specified by TEnum to an equivalent enumerated object.

Parse(Type, ReadOnlySpan<Char>)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

Converts the span of characters representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

C#
public static object Parse(Type enumType, ReadOnlySpan<char> value);

Parameters

enumType
Type

An enumeration type.

value
ReadOnlySpan<Char>

A span containing the name or value to convert.

Returns

An object of type enumType whose value is represented by value.

Exceptions

enumType is null.

enumType is not an Enum.

value is either an empty string or only contains white space.

value is a name, but not one of the named constants defined for the enumeration.

value is outside the range of the underlying type of enumType.

.NET 8 and later versions: enumType is a Boolean-backed enumeration type.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

Parse(Type, String)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

C#
public static object Parse(Type enumType, string value);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public static object Parse(Type enumType, string value);

Parameters

enumType
Type

An enumeration type.

value
String

A string containing the name or value to convert.

Returns

An object of type enumType whose value is represented by value.

Attributes

Exceptions

enumType or value is null.

enumType is not an Enum.

-or-

value is either an empty string or only contains white space.

-or-

value is a name, but not one of the named constants defined for the enumeration.

value is outside the range of the underlying type of enumType.

.NET 8 and later versions: enumType is a Boolean-backed enumeration type.

Examples

The following example uses the Parse(Type, String) method to parse an array of strings that are created by calling the GetNames method. It also uses the Parse(Type, String) method to parse an enumeration value that consists of a bit field.

C#
using System;

public class ParseTest
{
    [Flags]
    enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };

    public static void Main()
    {
        Console.WriteLine("The entries of the Colors enumeration are:");
        foreach (string colorName in Enum.GetNames(typeof(Colors)))
        {
            Console.WriteLine("{0} = {1:D}", colorName,
                                         Enum.Parse(typeof(Colors), colorName));
        }
        Console.WriteLine();

        Colors orange = (Colors) Enum.Parse(typeof(Colors), "Red, Yellow");
        Console.WriteLine("The orange value {0:D} has the combined entries of {0}",
                           orange);
    }
}

/*
This code example produces the following results:

The entries of the Colors Enum are:
Red = 1
Green = 2
Blue = 4
Yellow = 8

The orange value 9 has the combined entries of Red, Yellow

*/

Remarks

The value parameter contains the string representation of an enumeration member's underlying value or named constant, or a list of named constants delimited by commas (,). One or more blank spaces can precede or follow each value, name, or comma in value. If value is a list, the return value is the value of the specified names combined with a bitwise OR operation.

If value is a name that does not correspond to a named constant of enumType, the method throws an ArgumentException. If value is the string representation of an integer that does not represent an underlying value of the enumType enumeration, the method returns an enumeration member whose underlying value is value converted to an integral type. If this behavior is undesirable, call the IsDefined method to ensure that a particular string representation of an integer is actually a member of enumType. The following example defines a Colors enumeration, calls the Parse(Type, String) method to convert strings to their corresponding enumeration values, and calls the IsDefined method to ensure that particular integral values are underlying values in the Colors enumeration.

C#
using System;

[Flags] enum Colors { None=0, Red = 1, Green = 2, Blue = 4 };

public class Example
{
   public static void Main()
   {
      string[] colorStrings = { "0", "2", "8", "blue", "Blue", "Yellow", "Red, Green" };
      foreach (string colorString in colorStrings)
      {
         try {
            Colors colorValue = (Colors) Enum.Parse(typeof(Colors), colorString);
            if (Enum.IsDefined(typeof(Colors), colorValue) | colorValue.ToString().Contains(","))
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString());
            else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString);
         }
         catch (ArgumentException) {
            Console.WriteLine("'{0}' is not a member of the Colors enumeration.", colorString);
         }
      }
   }
}
// The example displays the following output:
//       Converted '0' to None.
//       Converted '2' to Green.
//       8 is not an underlying value of the Colors enumeration.
//       'blue' is not a member of the Colors enumeration.
//       Converted 'Blue' to Blue.
//       'Yellow' is not a member of the Colors enumeration.
//       Converted 'Red, Green' to Red, Green.

This operation is case-sensitive.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Parse(Type, ReadOnlySpan<Char>, Boolean)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

Converts the span of characters representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. A parameter specifies whether the operation is case-insensitive.

C#
public static object Parse(Type enumType, ReadOnlySpan<char> value, bool ignoreCase);

Parameters

enumType
Type

An enumeration type.

value
ReadOnlySpan<Char>

A span containing the name or value to convert.

ignoreCase
Boolean

true to ignore case; false to regard case.

Returns

An object of type enumType whose value is represented by value.

Exceptions

enumType is null.

enumType is not an Enum.

value is either an empty string or only contains white space.

value is a name, but not one of the named constants defined for the enumeration.

value is outside the range of the underlying type of enumType

.NET 8 and later versions: enumType is a Boolean-backed enumeration type.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

Parse(Type, String, Boolean)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. A parameter specifies whether the operation is case-insensitive.

C#
public static object Parse(Type enumType, string value, bool ignoreCase);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public static object Parse(Type enumType, string value, bool ignoreCase);

Parameters

enumType
Type

An enumeration type.

value
String

A string containing the name or value to convert.

ignoreCase
Boolean

true to ignore case; false to regard case.

Returns

An object of type enumType whose value is represented by value.

Attributes

Exceptions

enumType or value is null.

enumType is not an Enum.

-or-

value is either an empty string ("") or only contains white space.

-or-

value is a name, but not one of the named constants defined for the enumeration.

value is outside the range of the underlying type of enumType.

.NET 8 and later versions: enumType is a Boolean-backed enumeration type.

Examples

The following example uses the Parse(Type, String, Boolean) method to parse an array of strings that are created by calling the GetNames method. It also uses the Parse(Type, String) method to parse an enumeration value that consists of a bit field.

C#
using System;

[Flags] enum Colors { None=0, Red = 1, Green = 2, Blue = 4 };

public class Example
{
   public static void Main()
   {
      string[] colorStrings = { "0", "2", "8", "blue", "Blue", "Yellow", "Red, Green" };
      foreach (string colorString in colorStrings)
      {
         try {
            Colors colorValue = (Colors) Enum.Parse(typeof(Colors), colorString, true);
            if (Enum.IsDefined(typeof(Colors), colorValue) | colorValue.ToString().Contains(","))
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString());
            else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString);
         }
         catch (ArgumentException) {
            Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString);
         }
      }
   }
}
// The example displays the following output:
//       Converted '0' to None.
//       Converted '2' to Green.
//       8 is not an underlying value of the Colors enumeration.
//       Converted 'blue' to Blue.
//       Converted 'Blue' to Blue.
//       Yellow is not a member of the Colors enumeration.
//       Converted 'Red, Green' to Red, Green.

Remarks

The value parameter contains the string representation of an enumeration member's underlying value or named constant, or a list of named constants delimited by commas (,). One or more blank spaces can precede or follow each value, name, or comma in value. If value is a list, the return value is the value of the specified names combined with a bitwise OR operation.

If value is a name that does not correspond to a named constant of enumType, the method throws an ArgumentException. If value is the string representation of an integer that does not represent an underlying value of the enumType enumeration, the method returns an enumeration member whose underlying value is value converted to an integral type. If this behavior is undesirable, call the IsDefined method to ensure that a particular string representation of an integer is actually a member of enumType. The following example defines a Colors enumeration, calls the Parse(Type, String, Boolean) method to convert strings to their corresponding enumeration values, and calls the IsDefined method to ensure that particular integral values are underlying values in the Colors enumeration.

The ignoreCase parameter specifies whether this operation is case-sensitive.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Parse<TEnum>(String, Boolean)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

Converts the string representation of the name or numeric value of one or more enumerated constants specified by TEnum to an equivalent enumerated object. A parameter specifies whether the operation is case-insensitive.

C#
public static TEnum Parse<TEnum>(string value, bool ignoreCase) where TEnum : struct;

Type Parameters

TEnum

An enumeration type.

Parameters

value
String

A string containing the name or value to convert.

ignoreCase
Boolean

true to ignore case; false to regard case.

Returns

TEnum

An object of type TEnum whose value is represented by value.

Exceptions

TEnum is not an Enum type.

value is null.

value does not contain enumeration information.

.NET 8 and later versions: TEnum is a Boolean-backed enumeration type.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Parse<TEnum>(ReadOnlySpan<Char>, Boolean)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

Converts the span of characters representation of the name or numeric value of one or more enumerated constants specified by TEnum to an equivalent enumerated object. A parameter specifies whether the operation is case-insensitive.

C#
public static TEnum Parse<TEnum>(ReadOnlySpan<char> value, bool ignoreCase) where TEnum : struct;

Type Parameters

TEnum

An enumeration type.

Parameters

value
ReadOnlySpan<Char>

A span containing the name or value to convert.

ignoreCase
Boolean

true to ignore case; false to regard case.

Returns

TEnum

TEnum An object of type TEnum whose value is represented by value.

Exceptions

TEnum is not an Enum type.

value does not contain enumeration information.

.NET 8 and later versions: TEnum is a Boolean-backed enumeration type.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

Parse<TEnum>(ReadOnlySpan<Char>)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

Converts the span of characters representation of the name or numeric value of one or more enumerated constants specified by TEnum to an equivalent enumerated object.

C#
public static TEnum Parse<TEnum>(ReadOnlySpan<char> value) where TEnum : struct;

Type Parameters

TEnum

An enumeration type.

Parameters

value
ReadOnlySpan<Char>

A span containing the name or value to convert.

Returns

TEnum

TEnum An object of type TEnum whose value is represented by value.

Exceptions

TEnum is not an Enum type.

value does not contain enumeration information.

.NET 8 and later versions: TEnum is a Boolean-backed enumeration type.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

Parse<TEnum>(String)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

Converts the string representation of the name or numeric value of one or more enumerated constants specified by TEnum to an equivalent enumerated object.

C#
public static TEnum Parse<TEnum>(string value) where TEnum : struct;

Type Parameters

TEnum

An enumeration type.

Parameters

value
String

A string containing the name or value to convert.

Returns

TEnum

An object of type TEnum whose value is represented by value.

Exceptions

TEnum is not an Enum type.

value is null.

value does not contain enumeration information.

.NET 8 and later versions: TEnum is a Boolean-backed enumeration type.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1