Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C#
C# Reference
C# Keywords
Types
Value Types
 enum
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:

Want more? Here are some additional resources on this topic:

C# Language Reference
enum (C# Reference)

The enum keyword is used to declare an enumeration, a distinct type consisting of a set of named constants called the enumerator list. Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. For example:

      enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

In this enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth. Enumerators can have initializers to override the default values. For example:

      enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

In this enumeration, the sequence of elements is forced to start from 1 instead of 0.

A variable of type Days can be assigned any value in the range of the underlying type; the values are not limited to the named constants.

The default value of an enum E is the value produced by the expression (E)0.

NoteNote

An enumerator may not contain white space in its name.

The underlying type specifies how much storage is allocated for each enumerator. However, an explicit cast is needed to convert from enum type to an integral type. For example, the following statement assigns the enumerator Sun to a variable of the type int using a cast to convert from enum to int:

int x = (int)Days.Sun;

When you apply System.FlagsAttribute to an enumeration that contains some elements combined with a bitwise OR operation, you will notice that the attribute affects the behavior of the enum when used with some tools. You can notice these changes when using tools such as the Console class methods, the Expression Evaluator, and so forth. (See example 3).

Assigning additional values new versions of enums, or changing the values of the enum members in a new version, can cause problems for dependant source code. It is often the case that enum values are used in switch statements, and if additional elements have been added to the enum type, the test for default values can return true unexpectedly.

If other developers will be using your code, it is important to provide guidelines on how their code should react if new elements are added to any enum types.

In this example, an enumeration, Days, is declared. Two enumerators are explicitly converted to integer and assigned to integer variables.

// keyword_enum.cs
// enum initialization:
using System;
public class EnumTest 
{
    enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

    static void Main() 
    {
        int x = (int)Days.Sun;
        int y = (int)Days.Fri;
        Console.WriteLine("Sun = {0}", x);
        Console.WriteLine("Fri = {0}", y);
    }
}

Output

Sun = 2
Fri = 7

In this example, the base-type option is used to declare an enum whose members are of the type long. Notice that even though the underlying type of the enumeration is long, the enumeration members must still be explicitly converted to type long using a cast.

// keyword_enum2.cs
// Using long enumerators
using System;
public class EnumTest 
{
    enum Range :long {Max = 2147483648L, Min = 255L};
    static void Main() 
    {
        long x = (long)Range.Max;
        long y = (long)Range.Min;
        Console.WriteLine("Max = {0}", x);
        Console.WriteLine("Min = {0}", y);
    }
}

Output

Max = 2147483648
Min = 255

The following code example illustrates the use and effect of the System.FlagsAttribute attribute on an enum declaration.

// enumFlags.cs
// Using the FlagsAttribute on enumerations.
using System;

[Flags]
public enum CarOptions
{
    SunRoof = 0x01,
    Spoiler = 0x02,
    FogLights = 0x04,
    TintedWindows = 0x08,
}

class FlagTest
{
    static void Main()
    {
        CarOptions options = CarOptions.SunRoof | CarOptions.FogLights;
        Console.WriteLine(options);
        Console.WriteLine((int)options);
    }
}

Output

SunRoof, FogLights
5

Notice that if you remove the initializer from Sat=1, the result will be:

Sun = 1
Fri = 6

Notice that if you remove FlagsAttribute, the example will output the following:

5

5

For more information, see the following sections in the C# Language Specification:

  • 1.10 Enums

  • 6.2.2 Explicit Enumeration Conversions

  • 14 Enums

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Implicit Conversion      nbove   |   Edit   |   Show History

Please allow implicit enum conversion in C#. Forcing explicit conversion does not help code readability, in fact it does exactly the opposite.

Tags What's this?: Add a tag
Flag as ContentBug
I'm with #1: an explicit cast is needed      bav deLux ... Stanley Roark   |   Edit   |   Show History

what's the sense in:
switch((MyType)SomeInt)
{
case MyType.E0:
SomeOtherInt = (int)MyType.E1;
break;
}

wouldn't it be nicer this way:
switch(SomeInt)
{
case MyType.E0:
SomeOtherInt = MyType.E1;
break;
}
?


??

Tags What's this?: Add a tag
Flag as ContentBug
same as #1 and #2      Jonathan Conley   |   Edit   |   Show History
enum is nothing more then a named int or whatever type you set it to having to cast it after that makes no sense.
Tags What's this?: Add a tag
Flag as ContentBug
Disagreement with all of the above      Josh Rosenberg [MSFT]   |   Edit   |   Show History
enums should not do implicit conversion (at least, not by default), because then their primary raison d'etre is eliminated. enums were created to enable easy compiler-time checks to ensure only accepted values were provided to a function or other construct, and to enable the use of names in cases where numerics don't matter. If they convert seamlessly to and from the underlying type, then you may as well use just declare int constants and be done with it, because you'll be back where we started, and enums will just be a short hand for a list of constants.
Tags What's this?: Add a tag
Flag as ContentBug
I was wondering      salmanjamali ... sathyahs   |   Edit   |   Show History

I was wondering if enums in C# could possibly be used to do the following:

BookType {
HARDCOPY = "expensive",
EBOOK = "cheap"
}

String bookTypePriceIdea = (String) BookType.HARDCOPY;

To ShadowRanger      Leahn   |   Edit   |   Show History
ShadowRanger, enum, as per the documentation, are substituted for their respecitve numeric value during compiler time, which means that your program knows no enum, only its values, so yes, implicit conversion makes sense. It is already a constant of type int (or whatever type your assigned to it) so casting it should not be needed.

Also, enum in C#, unlike their counterparts in other languages DO NOT enforce the accepted values, if you declare an enum with values ranging from 1 to 7, and I pass a "(<enumName>)10 " to your function as a parameter, it will be accepted. C/C++ overlap the values, so if I passed 10, the compiler would switch it to 3 during compiling time. C# does not do this.

To finish, enums are ONLY int constants, the purpose of their name is simply to make it easier for you to remember the correct value to pass (easier to remember 'ReturnValue.Success' than '5'). Any other language with much tigher enum specification allow free conversion from the enum to its underlying type. C/C++ even allow you do to aritmetic operations straight with the enum values (and overlap the result accordingly). VB.NET allows free conversion to the underlying type. There is no reason to not to allow it as well in C#.


Tags What's this?: Add a tag
Flag as ContentBug
To salmanjamali      Leahn ... pbnec   |   Edit   |   Show History
You can attain what you want with the following syntax:

public static BookType

{

public const string HARDCOPY = "expensive";

public const string EBOOK = "cheap";

}


For Leahn and possilby the other questions      Kozmoknot   |   Edit   |   Show History
Your code doesn't work...it needs the "struct" value type specified instead of "static":

public struct BookType

{

public const string HARDCOPY = "expensive";

public const string EBOOK = "cheap";

}


A struct may also be used instead of an enum to accomplish implicit conversions to an integral type.
Tags What's this?: Add a tag
Flag as ContentBug
Quick Note to Salmanjamli, Leahn and Kozmoknot      Dave Conner   |   Edit   |   Show History

Perhaps Leahn simply left out the keyword class in her code.

public static class BookType
{
public const string HARDCOPY = "expensive";
public const string EBOOK = "cheap";
public const string kindle = "other";
}
certainly would work.

However I agree with Kozmoknot - if this is the extent of the members there is no reason to use a class. One would be better off using a struct. In the CLR, a struct is a value type, is lighter weight and allocated directly from the stack.

Tags What's this?: Add a tag
Flag as ContentBug
Extendable enums      Cristian Chelaru   |   Edit   |   Show History
I nice feature, and not too hard to implement (at least in C#, since Enum is actually a class), would be enum extendability. More than once had I wished for some enum values available in one enum to be present in another one, along with others - custom defined. Some very relevant examples would be:

1. Creating customized message boxes, with customized DialogResult, MessageBoxButtons and MessageBoxIcon. It would have been much simpler and elegant, had this feature been implemented.
2. Creating customized exceptions, and defining an ErrorCode property, which - if defined as an enum type - would allow extended exceptions to use the same error codes, and also add their own, very elegant and easy.
MS failed on enums      etki   |   Edit   |   Show History

why did MS mess up such a a basic concept introduced in the early 80's... maybe even earlier...?

i can't imagine any more common uses than switch statements and as indexes into arrays and vector types.

so, why the (int) type casting?

if, MS was really smart, they would have just copied Ada's implementation ... i.e. 'Pos, 'Val, 'First, 'Last, etc

why reinvent a bad wheel... a square one.

Magical "0" value for enums      I2055   |   Edit   |   Show History
Try this one on for size. You can pass a zero as an enum even if there is no 0 defined (.net 2.0). If you change the 0 to anything else, even a defined integer in the enum, the program will no longer compile. Just what is the thinking here with the magical 0? FAIL

namespace Fail
{
class Program
{
static void Main(string[] args)
{
var f = new Fail(0);
}
}

public class Fail
{
private SomeEnum mEnu;
public Fail(SomeEnum enu)
{
mEnu = enu;
}
}

public enum SomeEnum
{
Critical = 1,
Important = 2,
Warning = 3,
Information = 4
}
}
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker