Professional Documents
Culture Documents
Lesson17 PDF
Lesson17 PDF
LESSON 17 - ENUMS
B E G I N N E R S ’ S
C# TUTORIAL
1 7 . E N U M S
WWW.CSHARP–STATION.COM
PAGE 1 OF 9
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 17 - ENUMS
This lesson explains how to use C# enums. Our objectives are as follows:
Enums are strongly typed constants. They are essentially unique types that
allow you to assign symbolic names to integral values. In the C# tradition,
they are strongly typed, meaning that an enum of one type may not be
implicitly assigned to an enum of another type even though the underlying
value of their members are the same. Along the same lines, integral types
and enums are not implicitly interchangable. All assignments between
different enum types and integral types require an explicit cast.
Enums are value types, which means they contain their own value, can't
inherit or be inherited from, and assignment copies the value of one enum
to another. You will see in this lesson and elsewhere that enums are used
and referred to with both lower case, enum, and upper case,
Enum. Strangely enough, the relationship between the two is that the C#
type, enum, inherits the Base Class Library (BCL) type, Enum. Use the C#
type, enum, to define new enums and use the BCL type, Enum, to
implement static enum methods.
The .NET Framework BCL contains many enums and examples of how they
are used. For example, every time you put an icon on a MessageBox, you
use the MessageBoxIcons enum. For a list of available enums in the BCL,
look at the documentation for the Enum class and click on the Derived
Classes link.
Whenever there are situations where you are using a set of related numbers
in a program, consider replacing those numbers with enums. It will make a
program more readable and type safe. Listing 17-1 contains an enum
definition and code that uses that enum in a switch statement. Instead of
using the numbers 0, 1, and 2 in the switch statement, the code is more
meaningful through the use of the Volume enum.
PAGE 2 OF 9
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 17 - ENUMS
using System;
class EnumSwitch
{
static void Main()
{
// create and initialize
// instance of enum type
Volume myVolume = Volume.Medium;
Any time a member of the Volume enum is used, it is fully qualified with the
"Volume" identifier to guarantee type safety. For example, if there were a
Meat enum in scope, then Meat.Medium would definitely have different
PAGE 3 OF 9
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 17 - ENUMS
Another modification you can make to an enum is to set the value of any
enum member. By default, the first member of an enum takes the value of
zero. If this value doesn't make sense for your enum, you can change it to
one or some other number. Additionally, you can change any of the
members of an enum to any value that is valid for its base type. Unassigned
enum members have a value that is one more than their predecessor. Listing
17-2 shows how to modify the base type and member values of an enum.
using System;
class EnumBaseAndMembers
{
static void Main()
{
// create and initialize
// instance of enum type
Volume myVolume = Volume.Low;
PAGE 4 OF 9
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 17 - ENUMS
case Volume.High:
Console.WriteLine("The volume has been turned up.");
break;
}
Console.ReadLine();
}
}
The Volume enum in Listing 17-2 shows how to modify the base type and
members of an enum. Its base type is changed to byte with the : <type>
syntax following the enum identifier, Volume. This ensures that the Volume
enum may only have members with values that are valid for type byte.
The first member of the Volume enum, Low, has its value changed to 1. The
same syntax, <member> = <value>, may be applied to any member of the
enum. You are restricted from creating forward references, circular
references, and duplicate references in enum members.
The default values of the Volume enum are Low=0, Medium=1, and High=2
because the first member of an enum defaults to 0 and the following
members default to one more than their predecessor. However, the Volume
enum in Listing 17-2 has its Low member set to 1, which means that
Medium=2 and High=3.
Enum types implicitly inherit the System.Enum type in the Base Class Library
(BCL). This also means that you can use the members of System.Enum to
operate on enum types. This section does just that, showing some useful
tips and tricks to use with enums in your programs.
using System;
PAGE 5 OF 9
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 17 - ENUMS
Console.ReadLine();
}
Console.Write(@"
1 - Low
2 - Medium
3 - High
Console.WriteLine();
PAGE 6 OF 9
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 17 - ENUMS
break;
}
Console.WriteLine();
}
The GetEnumFromUser method shows how to obtain int input and translate
it to an appropriate enum type. Converting an int to an enum makes the
code more readable and type safe. The following is an excerpt from Listing
17-3 that shows the pertinent part of the code that performs the conversion:
PAGE 7 OF 9
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 17 - ENUMS
After the program displays a menu, it prompts the user for a selection in the
form of a number (1, 2, or 3). When the user makes a selection and presses
the Enter key, the code reads the value with Console.ReadLine, which
returns the value as a string type. Since you can only cast an int to a
Volume enum type, the user's input must be converted from a string to an
int with the Int32.Parse method. Converting the int to a Volume enum type
is simply a matter of applying a cast operation during assignment.
To get all the members of an enum at the same time, you can use the
GetNames method of the System.Enum type, which returns a string array of
the names of all an enum's members. An exerpt from the
ListEnumMembersByName method in Listing 17.3 that shows this appears
below:
Instead of getting names of all the members of an enum, you may have a
reason to get all the values of the enum at one time. The code below, from
the ListEnumMembersByValue method in Listing 17.3, shows how to
accomplish this:
Given the type of the enum, the GetValues method of System.Enum will
return an array of the given enum's base type, which in this case is
PAGE 8 OF 9
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 17 - ENUMS
byte. While iterating through this list, each member is printed to the
console showing its value and name. The name is obtained by using the
GetName method of System.Enum, which accepts an enum type and value
for which to get the corresponding name of.
Enums are lists of strongly typed constants with members that are symbolic
names, corresponding to an underlying integral type. Enum base types can
be changed and member values can be specified. The System.Enum BCL
type is the base class of enum types and contains methods that allow you to
work with enums in different ways, such as working with a list of names or
values, converting from value to name, and converting from name to value.
For more information on the System.Enum type, see the .NET Framework
SDK documentation.
PAGE 9 OF 9
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED