savastano
Crypto Investor
Divine
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
Objective
Many things in the real-world conceptually represent enumerations: the days of the
week, months of the year, seasons, oceans, and compass directions, to name a few. In this tutorial we will show you how to use Enumeration in C# language.
Let's go
Definition of Enumeration
Enumeration (or just an enum for short), is very important in C# language. It mirrors the nature of the world in the programming language. See if you want to describe a week with all days of it, the easy way is to declare a enumeration with all days that belong to it.
In the above code, we declare a enumeration named Week and seven fields in that enumeration, each is separated by a comma. You can write many of fields in the body of an enum with no restrict.
When you want to refer to a field value in the enumeration, you just refer it directly from the enumeration name . Let see the example bellow:
In this example we have a function named printDaysOfWeek with an input parameter named value_of_day. This input parameter is a Days enumeration type. In body of function, we compare the input value with each of value of enumeration. If true, so we print to console.
Enumeration with Multiple Named Values
In Enumeration, you can declare multiple named with same value. The following snippet code declares two fileds named Wednesday and DupWednesday that have the same value.
By default, the compiler will set the first filed value of Enumeration is 0, and the next field value is increased sequentially. You can make your enumeration more clearly by assigning the value to each field:
Underlying Enumeration Types
By default, compiler makes the value type of each fields is int (integer) as a default type. If you want to change that default type, you can declare target type directly after the name of Enumeration. Note that you can only specify some types of enumeration include byte, short, int, long, sbyte, ushort, uint, or ulong. Bellow is an example of how to change default type:
Base on the type of enumeration, the compiler will specify the range of value that match the type of enumeration field. For example, this above enumeration will have range of value from 0 to 255 for each value of field.
Summary
In this tutorial we have show you about enumeration, includes:
Book traversal links for Using Enumeration in C#
Many things in the real-world conceptually represent enumerations: the days of the
week, months of the year, seasons, oceans, and compass directions, to name a few. In this tutorial we will show you how to use Enumeration in C# language.
Let's go
Definition of Enumeration
Enumeration (or just an enum for short), is very important in C# language. It mirrors the nature of the world in the programming language. See if you want to describe a week with all days of it, the easy way is to declare a enumeration with all days that belong to it.
- public
enum
Week
- {
- Sunday,
- Monday,
- Tuesday,
- Wednesday,
- Thursday,
- Friday,
- Saturday
- }
In the above code, we declare a enumeration named Week and seven fields in that enumeration, each is separated by a comma. You can write many of fields in the body of an enum with no restrict.
When you want to refer to a field value in the enumeration, you just refer it directly from the enumeration name . Let see the example bellow:
- public
void
printDaysOfWeek(
Week value_of_day)
- {
- switch
(
value_of_day)
{
- case
Days.
Monday
:
- Console.
WriteLine
(
"This is Monday"
)
;
- break
;
- case
Days.
Tuesday
:
- Console.
WriteLine
(
"This is Tuesday"
)
;
- break
;
- case
Days.
Wednesday
:
- Console.
WriteLine
(
"This is Wednesday"
)
;
- break
;
- case
Days.
Thursday
:
- Console.
WriteLine
(
"This is Thursday"
)
;
- break
;
- case
Days.
Friday
:
- Console.
WriteLine
(
"This is Friday"
)
;
- break
;
- case
Days.
Saturday
:
- Console.
WriteLine
(
"This is Saturday"
)
;
- break
;
- case
Days.
Sunday
:
- Console.
WriteLine
(
"This is Sunday"
)
;
- break
;
- default
:
- Console.
WriteLine
(
"This is not a day"
)
;
- break
;
- }
- }
In this example we have a function named printDaysOfWeek with an input parameter named value_of_day. This input parameter is a Days enumeration type. In body of function, we compare the input value with each of value of enumeration. If true, so we print to console.
Enumeration with Multiple Named Values
In Enumeration, you can declare multiple named with same value. The following snippet code declares two fileds named Wednesday and DupWednesday that have the same value.
- public
enum
WeekWithMultipleNamedValues
- {
- Sunday,
- Monday,
- Tuesday,
- Wednesday,
- DupWednesday =
Wednesday,//the HumpDay field has same value with Wednesday
- Thursday,
- Friday,
- Saturday
- }
By default, the compiler will set the first filed value of Enumeration is 0, and the next field value is increased sequentially. You can make your enumeration more clearly by assigning the value to each field:
- public
enum
Week
- {
- Sunday =
0
,
- Monday =
1
,
- Tuesday =
2
,
- Wednesday =
3
,
- Thursday =
4
,
- Friday =
5
,
- Saturday =
6
,
- }
Underlying Enumeration Types
By default, compiler makes the value type of each fields is int (integer) as a default type. If you want to change that default type, you can declare target type directly after the name of Enumeration. Note that you can only specify some types of enumeration include byte, short, int, long, sbyte, ushort, uint, or ulong. Bellow is an example of how to change default type:
- enum
Week:
byte
- {
- Sunday,
- Monday,
- Tuesday,
- Wednesday,
- Thursday,
- Friday,
- Saturday,
- }
Base on the type of enumeration, the compiler will specify the range of value that match the type of enumeration field. For example, this above enumeration will have range of value from 0 to 255 for each value of field.
Summary
In this tutorial we have show you about enumeration, includes:
- How to declare a enumeration
- Multiple named value in enumeration
- How to change type of enumeration
Book traversal links for Using Enumeration in C#
- ‹ String formatting in C# Language
- Up
- Using Interface in C# Language ›