public abstract class Enum : ValueType, IComparable, IFormattable
Object
ValueType
EnumThis type implements IComparable and IFormattable.
mscorlib
BCL
Provides support for all enumeration types. Serves as the base class for all enumeration types.
A Enum is a distinct type with named constant members. Each enumeration type has a corresponding integral type called the underlying type of the enumeration type. This underlying type is required to be a system supplied integer type that is large enough to represent all values defined in the enumeration. A Enum declaration is allowed to explicitly declare any integral type other than Char as an underlying type. This includes Byte, SByte, Int16, Int32, Int64, UInt16, UInt32, and UInt64. A Enum declaration that does not explicitly declare an underlying type has an underlying type of Int32 .Enum derives from ValueType but is not a value type. Programming languages typically provide syntax to declare sets of a specified enumeration type consisting of named constants and their values.
It is possible to treat instances of a Enum as bit fields containing multiple values. For more information, see FlagsAttribute .
[Note: Enum provides methods to compare instances of enumeration types, convert the value of an instance to its String representation, convert the String representation of a number to an instance of the enumeration type, and create an instance of a specified enumeration and value.]
System Namespace
Enum Methods
Enum.CompareTo Method
Enum.Equals Method
Enum.Format Method
Enum.GetHashCode Method
Enum.GetName Method
Enum.GetNames Method
Enum.GetUnderlyingType Method
Enum.GetValues Method
Enum.IsDefined Method
Enum.Parse(System.Type, System.String) Method
Enum.Parse(System.Type, System.String, bool) Method
Enum.ToObject(System.Type, System.Object) Method
Enum.ToObject(System.Type, sbyte) Method
Enum.ToObject(System.Type, short) Method
Enum.ToObject(System.Type, int) Method
Enum.ToObject(System.Type, byte) Method
Enum.ToObject(System.Type, ushort) Method
Enum.ToObject(System.Type, uint) Method
Enum.ToObject(System.Type, long) Method
Enum.ToObject(System.Type, ulong) Method
Enum.ToString(System.IFormatProvider) Method
Enum.ToString(System.String, System.IFormatProvider) Method
Enum.ToString() Method
Enum.ToString(System.String) Method
public int CompareTo(object target);
Returns the sort order of the current instance compared to the specified Object.
- target
- An object to compare the current instance to.
A Int32 containing a value that reflects the sort order of the current instance as compared to target. The following table defines the conditions under which the returned value is a negative number, zero, or a positive number.
Return Value Description Any negative integer The value of the current instance is less than the value of target. Zero The value of the current instance is equal to the value of target. Any positive integer The value of the current instance is greater than the value of target, or target is null
.
Exception Type Condition ArgumentException target and the current instance are not of the same enumeration type.
[Note: This method is implemented to support the IComparable interface.]
System.Enum Class, System Namespace
public override bool Equals(object obj);
Determines whether the current instance and the specified Object represent the same type and value.
- obj
- An object to compare the current instance to.
true
if obj is of the same enumeration type and represents the same value as the current instance; otherwise,false
.
[Note: This method overrides System.Object.Equals(System.Object).]
System.Enum Class, System Namespace
public static string Format(Type enumType, object value, string format);
Converts the specified element of the specified enumeration type to its String representation using the specified format.
- enumType
- A Type that specifies the type of the enumeration of which value is a member.
- value
- The enumeration element to be converted.
- format
- A String that specifies the output format to use.
The String representation of the value of the enumeration element.
Exception Type Condition ArgumentNullException enumType, value or format is a null reference. ArgumentException enumType is not a Enum. -or-
value is neither of type enumType nor does it have the same underlying type as enumType.
FormatException format contains an invalid value.
For cross-platform portability, the only valid values for format are:
Format Description "G" or "g" If value is equal to a defined value of enumType , returns the element name defined for value. If the FlagsAttribute attribute is set on the Enum declaration and value is a built-in integer type and is equal to a summation of enumeration elements, the return value contains the element names in an unspecified order, separated by commas (e.g. "Red, Yellow"). Otherwise, value is returned in decimal format.
"X" or "x" Returns value in hexadecimal format, without a leading 0x. The value is padded with leading zeroes to ensure the returned value is at least eight digits in length. "F" or "f" Behaves identically to "G", except the FlagsAttribute
is not required to be present on the Enum declaration."D" or "d" Returns value in decimal format with no leading zeroes.
The following example demonstrates formatting enumeration values.
using System; public enum Signs { Stop = 1, Yield = 2, Merge = 4 }; [Flags] public enum Lights { Red = 1, Yellow = 2, Green = 4 }; public class EnumCompTo { public static void Main() { Console.WriteLine(Signs.Format(typeof(Signs), Signs.Merge, "d")); Console.WriteLine(Signs.Format(typeof(Signs), 7, "g")); Console.WriteLine(Lights.Format(typeof(Lights), Lights.Yellow, "x")); Console.WriteLine(Lights.Format(typeof(Lights), 7, "g")); } }The output is
4
7
00000002
Red, Yellow, Green
System.Enum Class, System Namespace
public override int GetHashCode();
Generates a hash code for the current instance.
A Int32 containing a hash code for the current instance.
The algorithm used to generate the hash code is unspecified.[Note: This method overrides System.Object.GetHashCode.]
System.Enum Class, System Namespace
public static string GetName(Type enumType, object value);
Retrieves the name of the constant of the specified enumeration type that has the specified value.
- enumType
- A Type that specifies the type of the enumeration.
- value
- A Object that contains the integral value or the name of an enumeration constant.
A String containing the name of the enumerated constant in enumType whose value is value, or a null reference if no such constant is found.
Exception Type Condition ArgumentNullException enumType or value is a null reference. ArgumentException enumType is not a Type that describes a Enum . -or-
value is neither of type enumType nor does it have the same underlying type as enumType.
The following example demonstrates the System.Enum.GetName(System.Type,System.Object) method.
using System; public enum Signs { Stop = 1, Yield = 2, Merge = 4 }; public class EnumCompTo { public static void Main() { Console.Write( "The name of the constant with the value 4 is: " ); Console.WriteLine( "{0}.", Signs.GetName(typeof(Signs), 4)); Console.Write( "The name of the constant with the value Stop is: " ); Console.WriteLine( "{0}.", Signs.GetName(typeof(Signs), Signs.Stop )); } }The output is
The name of the constant with the value 4 is: Merge.
The name of the constant with the value Stop is: Stop.
System.Enum Class, System Namespace
public static string[] GetNames(Type enumType);
Returns a zero-based, one-dimensional String array that contains the names of the constants of the specified enumeration type.
- enumType
- A Type that specifies the type of an enumeration.
A String vector of the names of the constants in enumType. The elements of the vector are sorted by the values of the enumerated constants.
Exception Type Condition ArgumentNullException enumType is a null reference. ArgumentException enumType is not a Type that describes a Enum .
The following example demonstrates the System.Enum.GetNames(System.Type) method.
using System; public enum Colors { Red, White, Blue }; public class enumGetNames { public static void Main() { int i = 0; String[] strAry = Colors.GetNames( typeof(Colors) ); foreach (String str in strAry) { Console.Write("The value indexed '{0}' ", i++ ); Console.WriteLine("is {0}.", str); } } }The output is
The value indexed '0' is Red.
The value indexed '1' is White.
The value indexed '2' is Blue.
System.Enum Class, System Namespace
public static Type GetUnderlyingType(Type enumType);
Returns the underlying type of the specified enumeration type.
- enumType
- The Type of an enumeration.
A Type instance that describes the underlying type of enumType.
Exception Type Condition ArgumentNullException enumType is a null reference. ArgumentException enumType is not an enumeration type.
The following example demonstrates the System.Enum.GetUnderlyingType(System.Type) method.
using System; public enum Colors { Red, White, Blue } public class EnumUnderlyingTypeTest { public static void Main() { Type t = Colors.GetUnderlyingType( typeof(Colors) ); Console.WriteLine("The underlying type of Colors is {0}.", t.ToString()); } }The output is
The underlying type of Colors is System.Int32.
System.Enum Class, System Namespace
public static Array GetValues(Type enumType);
Returns a zero-based, one-dimensional array of the values of the constants of the specified enumeration type.
- enumType
- The Type of an enumeration.
A vector of the enumeration type specified by enumType containing the values of the constants in enumType. The elements of the array are sorted by the values of the enumeration constants.
Exception Type Condition ArgumentNullException enumType is a null reference. ArgumentException enumType is not an enumeration type.
The following example demonstrates the System.Enum.GetValues(System.Type) method.
using System; public enum Colors { Red = 1, White = 2, Blue = 4 } public class enumGetValues { public static void Main() { Array valueAry = Enum.GetValues(typeof(Colors)); foreach (int i in valueAry) { Console.WriteLine("The value of element {0} is {1}", Enum.Format(typeof(Colors), i, "g"),i); } } }The output is
The value of element Red is 1.
The value of element White is 2.
The value of element Blue is 4.
System.Enum Class, System Namespace
public static bool IsDefined(Type enumType, object value);
Returns a Boolean indicating whether a constant with the specified value exists in the specified enumeration type.
- enumType
- A Type that describes an enumeration.
- value
- The constant or value being searched for in enumType.
true
if a constant in the enumeration described by enumType has a value equal to value; otherwise,false
.
Exception Type Condition ArgumentNullException enumType or value is a null reference. ArgumentException enumType is not an enumeration type. -or-
The type of value is not an enumType or an underlying type of enumType.
The following example demonstrates the System.Enum.IsDefined(System.Type,System.Object) method.
using System; public enum Colors { Red = 1, White = 2, Blue = 4 } public class enumIsDefined { public static void Main() { Console.Write("It is {0} ", Enum.IsDefined(typeof(Colors), 1)); Console.WriteLine("that '1' is defined in 'Colors'."); Console.Write("It is {0} ", Enum.IsDefined(typeof(Colors), 3)); Console.WriteLine("that '3' is defined in 'Colors'."); } }The output is
It is True that '1' is defined in 'Colors'.
It is False that '3' is defined in 'Colors'.
System.Enum Class, System Namespace
public static object Parse(Type enumType, string value);
Converts the specified String representation of one or more enumerated constants of a specified enumeration type to an equivalent enumerated object.
- enumType
- The Type of an enumeration.
- 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.
A Object of type enumType whose values are represented by value.
Exception Type Condition ArgumentNullException enumType or value is a null reference. ArgumentException enumType is not a Type that describes a Enum. -or-
value is either equal to System.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.
This version of System.Enum.Parse(System.Type,System.String) is equivalent to System.Enum.Parse(System.Type,System.String) (enumType, value,false
).
The following example demonstrates the System.Enum.Parse(System.Type,System.String) method.
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
System.Enum Class, System Namespace
public static object Parse(Type enumType, string value, bool ignoreCase);
Converts the specified String representation of one or more enumerated constants of a specified enumeration type to an equivalent enumerated object. This method behaves in a case-sensitive or insensitive manner according to the specified Boolean.
- enumType
- The Type of an enumeration.
- 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 or number can be surrounded by any amount of white space.
- ignoreCase
- A Boolean value. Specify
true
to have names in value parsed in a case-insensitive manner. Specifyfalse
to have names parsed in a case-sensitive manner.
A Object of type enumType whose values are represented by value.
Exception Type Condition ArgumentNullException enumType or value is a null reference. ArgumentException enumType is not a Type that describes a Enum. -or-
value is either equal to System.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.
For an example that demonstrates parsing strings containing enumeration values, see System.Enum.Parse(System.Type,System.String)(Type, String).
System.Enum Class, System Namespace
public static object ToObject(Type enumType, object value);
Returns an instance of the specified enumeration type set to the specified value.
- enumType
- The Type of the enumeration.
- value
- The value to set.
An enumeration object of type enumType whose value is value.
Exception Type Condition ArgumentNullException enumType is a null reference. ArgumentException enumType is not a Type that describes a Enum .
System.Enum Class, System Namespace
public static object ToObject(Type enumType, sbyte value);
Returns an instance of the specified enumeration type set to the specified SByte value.
- enumType
- The Type of an enumeration.
- value
- The SByte value to set.
An enumeration object of type enumType whose value is value.
Exception Type Condition ArgumentNullException enumType is a null reference. ArgumentException enumType is not a Type that describes a Enum .
This member is not CLS-compliant. For a CLS-compliant alternative, use System.Enum.ToObject(System.Type,System.Object)(Type, Int16).
CLSCompliantAttribute(false)
System.Enum Class, System Namespace
public static object ToObject(Type enumType, short value);
Returns an instance of the specified enumeration type set to the specified Int16 value.
- enumType
- The Type of an enumeration.
- value
- The Int16 value to set.
An enumeration object of type enumType whose value is value.
Exception Type Condition ArgumentNullException enumType is a null reference. ArgumentException enumType is not a Type that describes a Enum .
System.Enum Class, System Namespace
public static object ToObject(Type enumType, int value);
Returns an instance of the specified enumeration type set to the specified Int32 value.
- enumType
- The Type of an enumeration.
- value
- The Int32 value to set.
An enumeration object of type enumType whose value is value.
Exception Type Condition ArgumentNullException enumType is a null reference. ArgumentException enumType is not a Type that describes a Enum .
System.Enum Class, System Namespace
public static object ToObject(Type enumType, byte value);
Returns an instance of the specified enumeration type set to the specified Byte value.
- enumType
- The Type of an enumeration.
- value
- The Byte value to set.
An enumeration object of type enumType whose value is value.
Exception Type Condition ArgumentNullException enumType is a null reference. ArgumentException enumType is not a Type that describes a Enum .
System.Enum Class, System Namespace
public static object ToObject(Type enumType, ushort value);
Returns an instance of the specified enumeration type set to the specified UInt16 value.
- enumType
- The Type of an enumeration.
- value
- The UInt16 value to set.
An enumeration object of type enumType whose value is value.
Exception Type Condition ArgumentNullException enumType is a null reference. ArgumentException enumType is not a Type that describes a Enum .
This member is not CLS-compliant. For a CLS-compliant alternative, use System.Enum.ToObject(System.Type,System.Object)(Type, Int32).
CLSCompliantAttribute(false)
System.Enum Class, System Namespace
public static object ToObject(Type enumType, uint value);
Returns an instance of the specified enumeration type set to the specified UInt32 value.
- enumType
- The Type of an enumeration.
- value
- The UInt32 value to set.
An enumeration object of type enumType whose value is value.
Exception Type Condition ArgumentNullException enumType is a null reference. ArgumentException enumType is not a Type that describes a Enum .
This member is not CLS-compliant. For a CLS-compliant alternative, use System.Enum.ToObject(System.Type,System.Object)(Type, Int64).
CLSCompliantAttribute(false)
System.Enum Class, System Namespace
public static object ToObject(Type enumType, long value);
Returns an instance of the specified enumeration type set to the specified Int64 value.
- enumType
- The Type of an enumeration.
- value
- The Int64 value to set.
An enumeration object of type enumType whose value is value.
Exception Type Condition ArgumentNullException enumType is a null reference. ArgumentException enumType is not a Type that describes a Enum .
System.Enum Class, System Namespace
public static object ToObject(Type enumType, ulong value);
Returns an instance of the specified enumeration type set to the specified UInt64 value.
- enumType
- The Type of an enumeration.
- value
- The UInt64 value to set.
An enumeration object of type enumType whose value is value.
Exception Type Condition ArgumentNullException enumType is a null reference. ArgumentException enumType is not a Type that describes a Enum .
This member is not CLS-compliant. For a CLS-compliant alternative, use System.Enum.ToObject(System.Type,System.Object)(Type, Int64 ).
CLSCompliantAttribute(false)
System.Enum Class, System Namespace
public string ToString(IFormatProvider provider);
Converts the name of the value of the current instance to its equivalent String representation.
- provider
- An object that implements the IFormatProvider interface or a null reference. The IFormatProvider is not used in the implementation of this method. [Note: There is no IFormatProvider that corresponds to a Enum object; therefore, provider is not utilized by this method, and any IFormatProvider may be passed as a parameter.]
The String representation of the name of the value of the current instance.
This method is equivalent to the version of System.Enum.ToString(System.String) that takes no arguments.
System.Enum Class, System Namespace
public string ToString(string format, IFormatProvider provider);
Converts the numeric value of the current instance to its equivalent String representation using the specified format.
- format
- A String that specifies the format to use when converting the current instance to a String . Specify one of the following values in upper or lower case: "G", "D", "X", or "F" .
- provider
- An object that implements the IFormatProvider interface or a null reference. The IFormatProvider is not used in the implementation of this method. [Note: There is no IFormatProvider that corresponds to a Enum object; therefore, provider is not utilized by this method, and any IFormatProvider may be passed as a parameter.]
The String representation of the value of the current instance as specified by format.
Exception Type Condition FormatException format does not contain a valid format specifier.
If format is a null reference or an empty string, the return value is formatted using the general format specifier ("G").[Note: For details on the format specifiers used with an enumeration object, see System.Enum.Format(System.Type,System.Object,System.String) .
This method is implemented to support the IFormattable interface.
]
System.Enum Class, System Namespace
public override string ToString();
Converts the name of the value of the current instance to its equivalent String representation.
The String representation of the named constant specified by the current instance.
This version of System.Enum.ToString(System.String) is equivalent to System.Enum.ToString(System.String) ("G",null
).This method returns the same value as System.Enum.Format(System.Type,System.Object,System.String) with the "g" or "G" format option.
An instance of an enumeration is set to a named constant value. This method returns the name of the constant an instance is set to, not the value itself.
System.Enum Class, System Namespace
public string ToString(string format);
Converts the value of the current instance to its equivalent String representation using the specified format.
- format
- A String that specifies the format to use when converting the current instance to a String. Specify one of the following values in upper or lower case: "G", "D", "X", or "F" .
The String representation of the value of the current instance as specified by format.
Exception Type Condition FormatException format contains an invalid value.
If format is a null reference or an empty string, the return value is formatted using the general format specifier ("G").[Note: For details on the format specifiers used with an enumeration object, see System.Enum.Format(System.Type,System.Object,System.String) .]
System.Enum Class, System Namespace