Enum.TryParse(TEnum) Method (String, TEnum%)
Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. The return value indicates whether the conversion succeeded.
Assembly: mscorlib (in mscorlib.dll)
static member TryParse : value:string * result:'TEnum byref -> bool when 'TEnum : struct
The enumeration type to which to convert value.
- Type: System.String
The string representation of the enumeration name or underlying value to convert.
- Type: %
When this method returns, contains an object of type TEnum whose value is represented by value. This parameter is passed uninitialized.
Return ValueType: System.Boolean
true if the value parameter was converted successfully; otherwise, false.
is identical to the Parse(Type, String) method, except that instead of throwing an exception, it returns false if the conversion fails. It eliminates the need for exception handling when parsing the string representation of an enumeration value.
The value parameter contains the string representation of an enumeration member's underlying value or named constant, or a list of named constants or underlying values delimited by commas (,). If value includes multiple named constants or values, one or more blank spaces can precede or follow each value, name, or comma in value. If value is a list, result reflects the value of the specified names or underlying values combined with a bitwise OR operation. If value is the string representation of the name of an enumeration value, the comparison of value with enumeration names is case-sensitive.
If value is a name that does not correspond to a named constant of TEnum, the method returns false. If value is the string representation of an integer that does not represent an underlying value of the TEnum 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 TEnum.
The following example defines a Colors enumeration, calls the 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.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.