Enum Class
Provides the base class for enumerations.
For a list of all members of this type, see Enum Members.
System.Object
System.ValueType
System.Enum
Derived classes
[Visual Basic] <Serializable> MustInherit Public Class Enum Implements IComparable, IFormattable, IConvertible [C#] [Serializable] public abstract class Enum : IComparable, IFormattable, IConvertible [C++] [Serializable] public __gc __abstract class Enum : public IComparable, IFormattable, IConvertible [JScript] public Serializable abstract class Enum implements IComparable, IFormattable, IConvertible
Thread Safety
This type is safe for multithreaded operations.
Remarks
An Enum is a named constant whose underlying type is any integral type except Char. If no underlying type is explicitly declared, Int32 is used. Programming languages typically provide syntax to declare an enumeration that consists of a set of named constants and their values.
Class Enum is derived from class ValueType; that is, Enum is itself a reference type, not a value type.
Enum provides methods to compare instances of this class, convert the value of an instance to its string representation, convert the string representation of a number to an instance of this class, and create an instance of a specified enumeration and value.
You can also treat an Enum as a bit field. For more information, see FlagsAttribute.
This class inherits from ValueType, and implements the IComparable, IFormattable, and IConvertible interfaces. Use the Convert class for conversions instead of this class' explicit interface member implementation of IConvertible.
Example
The following code sample demonstrates the use of Enum:
[Visual Basic] Imports System Public Class EnumTest Enum Days Saturday Sunday Monday Tuesday Wednesday Thursday Friday End Enum 'Days Enum BoilingPoints Celcius = 100 Fahrenheit = 212 End Enum 'BoilingPoints <FlagsAttribute()> _ Enum Colors Red = 1 Green = 2 Blue = 4 Yellow = 8 End Enum 'Colors Public Shared Sub Main() Dim weekdays As Type = GetType(Days) Dim boiling As Type = GetType(BoilingPoints) Console.WriteLine("The days of the week, and their corresponding values in the Days Enum are:") Dim s As String For Each s In [Enum].GetNames(weekdays) Console.WriteLine("{0,-11} = {1}", s, [Enum].Format(weekdays, [Enum].Parse(weekdays, s), "d")) Next s Console.WriteLine() Console.WriteLine("Enums can also be created which have values that represent some meaningful amount.") Console.WriteLine("The BoilingPoints Enum defines the following items, and corresponding values:") For Each s In [Enum].GetNames(boiling) Console.WriteLine("{0,-11} = {1}", s, [Enum].Format(boiling, [Enum].Parse(boiling, s), "d")) Next s Dim myColors As Colors = Colors.Red Or Colors.Blue Or Colors.Yellow Console.WriteLine() Console.WriteLine("myColors holds a combination of colors. Namely: {0}", myColors) End Sub 'Main End Class 'EnumTest [C#] using System; public class EnumTest { enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday }; enum BoilingPoints { Celcius = 100, Fahrenheit = 212 }; [FlagsAttribute] enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 }; public static void Main() { Type weekdays = typeof(Days); Type boiling = typeof(BoilingPoints); Console.WriteLine("The days of the week, and their corresponding values in the Days Enum are:"); foreach ( string s in Enum.GetNames(weekdays) ) Console.WriteLine( "{0,-11}= {1}", s, Enum.Format( weekdays, Enum.Parse(weekdays, s), "d")); Console.WriteLine(); Console.WriteLine("Enums can also be created which have values that represent some meaningful amount."); Console.WriteLine("The BoilingPoints Enum defines the following items, and corresponding values:"); foreach ( string s in Enum.GetNames(boiling) ) Console.WriteLine( "{0,-11}= {1}", s, Enum.Format(boiling, Enum.Parse(boiling, s), "d")); Colors myColors = Colors.Red | Colors.Blue | Colors.Yellow; Console.WriteLine(); Console.WriteLine("myColors holds a combination of colors. Namely: {0}", myColors); } } [C++] #using <mscorlib.dll> using namespace System; __value enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday }; __value enum BoilingPoints { Celcius = 100, Fahrenheit = 212 }; [FlagsAttribute] __value enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 }; int main() { Type* weekdays = __typeof(Days); Type* boiling = __typeof(BoilingPoints); Console::WriteLine("The days of the week, and their corresponding values in the Days Enum are:"); Array* a = Enum::GetNames(weekdays); Int32 i = 0; do{ Object* o = a->GetValue(i); Console::WriteLine( "{0,-11}= {1}", o->ToString(), Enum::Format( weekdays, Enum::Parse(weekdays, o->ToString()), "d")); }while( ++i < a->Length); Console::WriteLine(); Console::WriteLine("Enums can also be created which have values that represent some meaningful amount."); Console::WriteLine("The BoilingPoints Enum defines the following items, and corresponding values:"); i = 0; Array* b = Enum::GetNames(boiling); do{ Object* o = b->GetValue(i); Console::WriteLine( "{0,-11}= {1}", o->ToString(), Enum::Format(boiling, Enum::Parse(boiling, o->ToString()), "d")); }while( ++i < b->Length ); Array* c = Enum::GetNames(__typeof(Colors)); Int32 myColors __gc[]= {Colors::Red, Colors::Blue, Colors::Yellow}; Console::WriteLine(); Console::Write("myColors holds a combination of colors. Namely:"); for( i=0; i<3; i++) Console::Write(" {0}", c->GetValue(i)); } [JScript] import System; public class EnumTest { enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday }; enum BoilingPoints { Celcius = 100, Fahrenheit = 212 }; FlagsAttribute enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 }; public static function Main() { var weekdays : Type = Days; var boiling : Type = BoilingPoints; Console.WriteLine("The days of the week, and their corresponding values in the Days Enum are:"); for( var i : int in Enum.GetNames(weekdays) ) Console.WriteLine( "{0,-11}= {1}", Enum.GetNames(weekdays).GetValue(i), Enum.Format( weekdays, Enum.Parse(weekdays, Enum.GetNames(weekdays).GetValue(i)), "d")); Console.WriteLine(); Console.WriteLine("Enums can also be created which have values that represent some meaningful amount."); Console.WriteLine("The BoilingPoints Enum defines the following items, and corresponding values:"); for ( var j : int in Enum.GetNames(boiling) ) Console.WriteLine( "{0,-11}= {1}", Enum.GetNames(boiling).GetValue(j), Enum.Format(boiling, Enum.Parse(boiling, Enum.GetNames(boiling).GetValue(j)), "d")); var myColors : Colors = Colors.Red | Colors.Blue | Colors.Yellow; Console.WriteLine(); Console.WriteLine("myColors holds a combination of colors. Namely: {0}", myColors); } }
Requirements
Namespace: System
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
Assembly: Mscorlib (in Mscorlib.dll)
See Also
Enum Members | System Namespace | ValueType | FlagsAttribute | Char | Int32