: Enumeration Type Enumeration Type Week Week 11 11: Monchai - - PDF document
: Enumeration Type Enumeration Type Week Week 11 11: Monchai - - PDF document
2/8/2007 204111 204111: Computer and : Computer and Programming Programming : Enumeration Type Enumeration Type Week Week 11 11: Monchai Sopitkamol, Ph.D. Monchai Sopitkamol, Ph.D. Enumeration Type Enumeration Type Motivations
2/8/2007 2
Motivations Motivations
Representing some group of symbolic
Representing some group of symbolic t t th i t i bl t t t th i t i bl t constants thru integer variables are not constants thru integer variables are not very intuitive. very intuitive.
E.g., representing colors (red, green, blue)
E.g., representing colors (red, green, blue) with 0, 1, and 2, respectively. with 0, 1, and 2, respectively.
E g
representing months (Jan Feb Mar E g representing months (Jan Feb Mar
E.g., representing months (Jan, Feb, Mar,
E.g., representing months (Jan, Feb, Mar, …, Dec) with 0, 1, 2, …, 11, respectively. …, Dec) with 0, 1, 2, …, 11, respectively.
Enumeration Type Enumeration Type
Motivations
Motivations
Declaring an Enumeration Type
Declaring an Enumeration Type
Choosing Enumeration Literal Values
Choosing Enumeration Literal Values
Choosing an Enumeration's Underlying
Choosing an Enumeration's Underlying Type Type
Using an Enumeration
Using an Enumeration
2/8/2007 3
Declaring an Enumeration Type Declaring an Enumeration Type
enum enum enum enum-
- type
type { literal literal-
- value
value1 1, , literal literal-
- value
value2 2, … } , … } E.g., E.g., enum Color { Red, Green, Blue }
enum Color { Red, Green, Blue }
- r
- r enum Month { Jan, Feb, Mar, …, Dec }
enum Month { Jan, Feb, Mar, …, Dec }
The symbolic names (literal values) must The symbolic names (literal values) must appear between a pair of braces ({ } ) in a appear between a pair of braces ({ } ) in a comma comma-
- separated list. These names
separated list. These names enumerate the values of a new type called enumerate the values of a new type called Color Color or
- r Month
Month that is an enumeration type. that is an enumeration type.
Enumeration Type Enumeration Type
Motivations
Motivations
Declaring an Enumeration Type
Declaring an Enumeration Type
Choosing Enumeration Literal Values
Choosing Enumeration Literal Values
Choosing an Enumeration's Underlying
Choosing an Enumeration's Underlying Type Type
Using an Enumeration
Using an Enumeration
2/8/2007 4
Choosing Enumeration Literal Values Choosing Enumeration Literal Values
Each enumeration has a set of values associated with
Each enumeration has a set of values associated with each element. each element.
By default, the numbering starts at
By default, the numbering starts at 0 0 for the first for the first element and goes up in steps of element and goes up in steps of 1 1. .
You can associate a specific integer constant (such as
You can associate a specific integer constant (such as 1 1) ) with an enumeration literal (such as Red), as in the with an enumeration literal (such as Red), as in the following example: following example:
enum Color { Red = enum Color { Red = 1 1, Green, Blue } , Green, Blue } I MPORTANT:
I MPORTANT: The integer value that you initialize an
The integer value that you initialize an en me ation lite al ith m st be a compile en me ation lite al ith m st be a compile time constant time constant enumeration literal with must be a compile enumeration literal with must be a compile-time constant time constant value (such as value (such as 1 1). That is, it must be a constant whose ). That is, it must be a constant whose value does not depend on any run value does not depend on any run-
- time behavior (such
time behavior (such as a method call). as a method call).
Choosing Enumeration Literal Values Choosing Enumeration Literal Values
If you don't explicitly give an enumeration literal
If you don't explicitly give an enumeration literal a constant integer value, the compiler gives it a a constant integer value, the compiler gives it a g , p g g , p g value that is one more than the value of the value that is one more than the value of the previous enumeration literal, except for the very previous enumeration literal, except for the very first enumeration literal, to which the compiler first enumeration literal, to which the compiler gives a value of gives a value of 0 0. .
E.g., in the previous version of
E.g., in the previous version of Color Color, the , the underlying values of Red, Green, and Blue are underlying values of Red, Green, and Blue are 1 1, , 2 and and 3 2, and , and 3. .
By default, the declaration “
By default, the declaration “enum Color { Red,
enum Color { Red, Green, Blue } Green, Blue }” has the values of Red, Green,
has the values of Red, Green, and Blue as and Blue as 0 0, , 1 1, and , and 2 2, respectively. , respectively.
2/8/2007 5
Choosing Enumeration Literal Values Choosing Enumeration Literal Values
You are allowed to give more than one
You are allowed to give more than one enumeration literal the same underlying value enumeration literal the same underlying value enumeration literal the same underlying value. enumeration literal the same underlying value. For example, you could code as follows: For example, you could code as follows:
“enum Color { Red, Green, Blue = Red }
enum Color { Red, Green, Blue = Red }”
BUT “ BUT “enum Color { Red, Green, Blue = White }
enum Color { Red, Green, Blue = White }” will
will yield compile yield compile-
- time error because
time error because White White does not exist does not exist in the enumeration! in the enumeration! ALSO “ ALSO “enum Color { Red = Blue, Green, Blue }
enum Color { Red = Blue, Green, Blue }” will give
” will give compile compile-
- time error either because of circular definition
time error either because of circular definition error! error!
Enumeration Type Enumeration Type
Motivations
Motivations
Declaring an Enumeration Type
Declaring an Enumeration Type
Choosing Enumeration Literal Values
Choosing Enumeration Literal Values
Choosing an Enumeration's Underlying
Choosing an Enumeration's Underlying Type Type
Using an Enumeration
Using an Enumeration
2/8/2007 6
Choosing an Enumeration's Choosing an Enumeration's Underlying Type Underlying Type
When you declare an enumeration type, the
When you declare an enumeration type, the enumeration literals are given values of type enumeration literals are given values of type int int enumeration literals are given values of type enumeration literals are given values of type int int. . In other words, the underlying type defaults to In other words, the underlying type defaults to an an int int. .
You can also choose to base your enumeration
You can also choose to base your enumeration type on any of the eight integer types: type on any of the eight integer types: byte, byte, sbyte, short, ushort, int, uint, long, sbyte, short, ushort, int, uint, long, or
- r ulong
ulong. F l t d l th t C l ' d l i F l t d l th t C l ' d l i
For example, to declare that Color's underlying
For example, to declare that Color's underlying type as a type as a short short rather than an rather than an int int, you can write: , you can write:
enum Color : short { Red, Green, Blue } enum Color : short { Red, Green, Blue }
Enumeration Type Enumeration Type
Motivations
Motivations
Declaring an Enumeration Type
Declaring an Enumeration Type
Choosing Enumeration Literal Values
Choosing Enumeration Literal Values
Choosing an Enumeration's Underlying
Choosing an Enumeration's Underlying Type Type
Using an Enumeration
Using an Enumeration
2/8/2007 7
Using an Enumeration Using an Enumeration
Once you have declared your enumeration
Once you have declared your enumeration type you can use the name of the type you can use the name of the type, you can use the name of the type, you can use the name of the enumeration as the name of a type in enumeration as the name of a type in exactly the same way as any other type. exactly the same way as any other type.
If
If Season Season is the name of your is the name of your enumeration type, you can create enumeration type, you can create variables variables of type
- f type Season
Season fields fields of type
- f type
variables variables of type
- f type Season
Season, , fields fields of type
- f type
Season Season, and , and parameters parameters of type
- f type Season
Season, , as shown in this example: as shown in this example:
Using an Enumeration Using an Enumeration
enum enum Season Season { { Spring, Spring, Summer, Summer, Fall, Fall, Winter Winter } } l E l E l class class Example Example { { public public void void Method(Season Method(Season parameter parameter) ) { { Season Season localVariable localVariable; ; } } } private private Season Season field field; ; } }
2/8/2007 8
Using an Enumeration Using an Enumeration
An enumeration type is a
An enumeration type is a value value type. This
- type. This
th t ti i bl h ld th t ti i bl h ld means that enumeration variables hold means that enumeration variables hold their values directly and live on the stack. their values directly and live on the stack.
The definite assignment rule applies to
The definite assignment rule applies to enumeration variables just as it applies to enumeration variables just as it applies to all other variables: all other variables: all other variables: all other variables:
Season Season colorful; colorful; Console.WriteLine(colorful); Console.WriteLine(colorful); // // compile compile-
- time
time error error
Using an Enumeration Using an Enumeration
Before you can use the value of an
Before you can use the value of an ti i bl it t b d fi it l ti i bl it t b d fi it l enumeration variable, it must be definitely enumeration variable, it must be definitely assigned a value, for example: assigned a value, for example:
Season colorful = Season.Fall; string name = colorful.ToString(); Console.WriteLine(name); // writes out 'Fall' Console WriteLine(colorful); // writes out 'Fall' Console.WriteLine(colorful); // writes out Fall
code
2/8/2007 9
Using an Enumeration Using an Enumeration
There are a few of points of interest in this
There are a few of points of interest in this example: example: example: example:
You have to write
You have to write Season.Fall Season.Fall rather than rather than Fall
- Fall. All
. All enumeration literal names are scoped inside their enumeration literal names are scoped inside their enumeration type. enumeration type.
You can convert an enumeration variable into a string
You can convert an enumeration variable into a string that contains the name of its enumeration literal that contains the name of its enumeration literal value by calling the value by calling the ToString ToString method on the method on the bl bl enumeration variable. enumeration variable.
When you write out an enumeration variable, the
When you write out an enumeration variable, the compiler automatically writes out the name of the compiler automatically writes out the name of the literal whose value matches the value of the variable. literal whose value matches the value of the variable.
Using an Enumeration Using an Enumeration
It's also possible to retrieve the underlying
It's also possible to retrieve the underlying i t l f ti i bl i t l f ti i bl integer value of an enumeration variable. integer value of an enumeration variable. To do this, you must cast it to its To do this, you must cast it to its underlying type. underlying type.
For example, the following code fragment
For example, the following code fragment will write out the value will write out the value 2, and not the , and not the will write out the value will write out the value 2, and not the , and not the word word Fall Fall:
enum Season { Spring, Summer, Fall, Winter } Season colorful = Season.Fall; Console.WriteLine((int)colorful); // writes out '2' code
2/8/2007 10
Using an Enumeration Using an Enumeration
All the standard operators that you can
All the standard operators that you can i t l i bl l b d i t l i bl l b d use on integral variables can also be used use on integral variables can also be used
- n enumeration variables.
- n enumeration variables.
For example, you can compare two
For example, you can compare two enumeration variables enumeration variables of the same type
- f the same type
for equality by using the = = operator. for equality by using the = = operator. for equality by using the
- perator.
for equality by using the
- perator.
Date example