: Enumeration Type Enumeration Type yp yp Week 11 Week 11: - - PDF document
: Enumeration Type Enumeration Type yp yp Week 11 Week 11: - - PDF document
204111: Computer and 204111 : Computer and Programming Programming Programming Programming : Enumeration Type Enumeration Type yp yp Week 11 Week 11: Monchai Sopitkamol, Ph.D. Monchai Sopitkamol, Ph.D. Enumeration Type Enumeration
Motivations Motivations Motivations Motivations
Representing some group of symbolic
Representing some group of symbolic constants thru integer variables are not constants thru integer variables are not g very intuitive. very intuitive.
E g
representing colors (red green blue) E g representing colors (red green blue)
E.g., representing colors (red, green, blue)
E.g., representing colors (red, green, blue) with with 0 0, , 1 1, and , and 2 2, respectively. , respectively.
E.g., representing months (Jan, Feb, Mar,
E.g., representing months (Jan, Feb, Mar, …, Dec) with …, Dec) with 0, 1, 2, …, , …, 11 11, respectively. , respectively. …, Dec) with …, Dec) with 0, , 1, , 2, …, , …, 11 11, respectively. , respectively.
Enumeration Type Enumeration Type Enumeration Type Enumeration Type
Motivations
Motivations
Declaring an Enumeration Type
Declaring an Enumeration Type
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 Type Type
Using an Enumeration
Using an Enumeration
Declaring an Enumeration Type Declaring an Enumeration Type Declaring an Enumeration Type Declaring an Enumeration Type
enum enum enum enum-type type { literal literal-value value1, , literal literal-value value2, … } , … } 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 }
- 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 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 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 Enumeration Type Enumeration Type
Motivations
Motivations
Declaring an Enumeration Type
Declaring an Enumeration Type
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 Type Type
Using an Enumeration
Using an Enumeration
Choosing Enumeration Literal Values Choosing Enumeration Literal Values 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 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 y , g y , g 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 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
I MPORTANT:
I MPORTANT: The integer value that you initialize an
The integer value that you initialize an 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 l d t d d l d t d d ti b h i ( h ti b h i ( h 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 Choosing Enumeration Literal Values Choosing Enumeration Literal Values
If d 't li itl i ti lit l If d 't li itl i ti lit l
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 value that is one more than the value of the value that is one more than the value of the 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 gives a value of gives a value of 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 underlying values of Red, Green, and Blue are underlying values of Red, Green, and Blue are 1, , 2, and , and 3 3. .
By default, the declaration “
By default, the declaration “enum Color { Red,
enum Color { Red,
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.
Choosing Enumeration Literal Values Choosing Enumeration Literal Values 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. 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 i th ti ! i th ti ! 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 compile compile-time error either because of circular definition time error either because of circular definition error! error!
Enumeration Type Enumeration Type Enumeration Type Enumeration Type
Motivations
Motivations
Declaring an Enumeration Type
Declaring an Enumeration Type
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 Type Type
Using an Enumeration
Using an Enumeration
Choosing an Enumeration's Choosing an Enumeration's Underlying Type Underlying Type
Wh d l ti t th Wh d l ti t th
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. . In other words the underlying type defaults to In other words the underlying type defaults to 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
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 sbyte, short, ushort, int, uint, long, sbyte, short, ushort, int, uint, long, or
- r ulong
ulong.
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: 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 Enumeration Type Enumeration Type
Motivations
Motivations
Declaring an Enumeration Type
Declaring an Enumeration Type
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 Type Type
Using an Enumeration
Using an Enumeration
Using an Enumeration Using an Enumeration Using an Enumeration Using an Enumeration
O h d l d i O h d l d i
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 h f h f 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 enumeration type, you can create enumeration type, you can create 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, 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 Using an Enumeration Using an Enumeration
{ F ll F ll } } enum enum Season Season { Spring, Spring, Summer, Summer, Fall, Fall, Winter Winter } } class class Example Example p { { public public void void Method(Season Method(Season parameter parameter) { { { { Season Season localVariable localVariable; ; } } } } private private Season Season field field; ; p } }
Using an Enumeration Using an Enumeration Using an Enumeration Using an Enumeration
An enumeration type is a
An enumeration type is a value value type. This
- type. This
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
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 ll h bl ll h bl all other variables: all other variables:
Season Season colorful; colorful; Season Season colorful; colorful; Console.WriteLine(colorful); Console.WriteLine(colorful); // // compile compile-
- time
time error error
Using an Enumeration Using an Enumeration Using an Enumeration Using an Enumeration
Before you can use the value of an
Before you can use the value of an enumeration variable, it must be definitely enumeration variable, it must be definitely , y , y 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(name); // writes out Fall Console.WriteLine(colorful); // writes out 'Fall'
code
Using an Enumeration Using an Enumeration Using an Enumeration Using an Enumeration
Th f f i t f i t t i thi Th f f i t f i t t i thi
There are a few of points of interest in this
There are a few of points of interest in this example: example:
Y h t it Y h t it S F ll S F ll th th th th F ll F ll All All
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. yp yp
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 l b lli th l b lli th T St i T St i th d th th d th value by calling the value by calling the ToString ToString method on the method on the enumeration variable. enumeration variable.
When you write out an enumeration variable the
When you write out an enumeration variable the
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 Using an Enumeration Using an Enumeration
It's also possible to retrieve the underlying
It's also possible to retrieve the underlying integer value of an enumeration variable. integer value of an enumeration variable. g To do this, you must cast it to its To do this, you must cast it to its underlying type underlying type underlying type. underlying type.
For example, the following code fragment
For example, the following code fragment ll h l ll h l 2 d h d h 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' Console.WriteLine((int)colorful); // writes out 2
code
Using an Enumeration Using an Enumeration Using an Enumeration Using an Enumeration
All the standard operators that you can
All the standard operators that you can use on integral variables can also be used use on integral variables can also be used g
- n enumeration variables.
- n enumeration variables.
For example you can compare two
For example you can compare two
For example, you can compare two
For example, you can compare two enumeration variables enumeration variables of the same type
- f the same type
f l b h f l b h for equality by using the = = operator. for equality by using the = = operator.
Date example