C# Enum Parse(Type, String, Boolean)

Description

Enum 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.

Syntax

Enum.Parse(Type, String, Boolean) has the following syntax.


[ComVisibleAttribute(true)]//from  w ww  .  j  a  v  a2 s. c om
public static Object Parse(
  Type enumType,
  string value,
  bool ignoreCase
)

Parameters

Enum.Parse(Type, String, Boolean) has the following parameters.

  • enumType - An enumeration type.
  • value - A string containing the name or value to convert.
  • ignoreCase - true to ignore case; false to regard case.

Returns

Enum.Parse(Type, String, Boolean) method returns An object of type enumType whose value is represented by value.

Example


using System;/* w  w w.  jav a 2  s .  c o  m*/

[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)
        {
            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);

        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version