Mono Class Library: System.Enum Overview | Members

System.Enum.Parse Method

Converts the specified string representation of one or more enumerated constants of a specified enumeration type to an equivalent enumerated object. [Edit]

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

Parameters

enumType
The Type of an enumeration. [Edit]
value
A string containing one or more names or a single numeric value to convert. If the string contains more than one name, each name is required to be separated by a comma (','). The names are parsed in a case-sensitive manner. The names or number can be surrounded by any amount of white space. [Edit]
value
Documentation for this section has not yet been entered. [Edit]

Returns

A object of type enumType whose values are represented by value. [Edit]

Exceptions

TypeReason
ArgumentNullExceptionenumType or value is a null reference. [Edit]
ArgumentException

enumType is not a Type that describes a Enum.

-or-

value is either equal to string.Empty or contains only white space.

-or-

value represents one or more names, and at least one name represented by value is not of type enumType.

[Edit]

Remarks

This version of Enum.Parse(Type, string) is equivalent to Enum.Parse(Type, string) (enumType, value, false). [Edit]

Example

The following example demonstrates the Enum.Parse(Type, string) method.

C# Example
using System;
public enum Colors {
  Red = 1,
  Blue = 2
}
public class enumTest {
  public static void Main() {
    object o = Enum.Parse( typeof (Colors), "Red, Blue");
    Type oType = o.GetType();
    Console.WriteLine("The type of the object returned is {0}",oType.ToString());
    Array values = Enum.GetValues(oType);
    foreach (Colors c in values) { 
      Console.WriteLine(Enum.Format(oType,c,"D"));
    }
  }
}

The output is

The type of the object returned is Colors
1
2

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0