Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Enumerators | Structs & Enumerators
C# Beyond Basics
course content

Course Content

C# Beyond Basics

C# Beyond Basics

1. Additional Structures & File Handling
2. Structs & Enumerators
3. Introduction to Object-Oriented Programming (OOP)
4. OOP Essentials
5. OOP Principles

bookEnumerators

Enum, also known as Enumerator, is a structure for grouping together some integral constants. The constants are basically of the type int.

Following is the syntax for creating an enum:

cs

index

copy
1234567
enum EnumName { Const1, Const2, Const3, ... }

By default the first element of an enum has the value 0, the second element has a value 1 and so on.

For-example:

cs

index

copy
12345678910
enum Days { Monday, // 0 Tuesday, // 1 Wednesday, // 2 Thursday, // 3 Friday, // 4 Saturday, // 5 Sunday // 6 }

We can access the values of these constants using the enumName.constantName syntax.

For-example:

cs

index

copy
123456789101112131415161718192021222324
using System; enum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } class ConsoleApp { static void Main(string[] args) { // Explicitly converting the enum elements to 'int' and then outputting them Console.WriteLine((int)Days.Monday); // 0 Console.WriteLine((int)Days.Wednesday); // 2 Console.WriteLine((int)Days.Friday); // 4 Console.WriteLine((int)Days.Sunday); // 6 } }

Note that in the above example we explicitly need to convert the constant into an int datatype since these constants are of type Days by default, which means that the enum Days is a new datatype which we created. Therefore, it can be stored into a variable of type Days:

This is useful in places like switch statements.

For-example:

cs

index

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
using System; enum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } class ConsoleApp { static void Main(string[] args) { Days day = Days.Friday; switch (day) { case Days.Monday: Console.WriteLine("It is Monday"); break; case Days.Tuesday: Console.WriteLine("It is Tuesday"); break; case Days.Wednesday: Console.WriteLine("It is Wednesday"); break; case Days.Thursday: Console.WriteLine("It is Thursday"); break; case Days.Friday: Console.WriteLine("It is Friday"); break; case Days.Saturday: Console.WriteLine("It is Saturday"); break; case Days.Sunday: Console.WriteLine("It is Sunday"); break; default: Console.WriteLine("Invalid Day"); break; } } }

We can also manually assign values to the enum constants. The unassigned constants take up incremented values of the previous elements:

cs

index

copy
123456789101112131415161718192021222324252627
using System; enum Days { Monday, Tuesday, Wednesday = 5, Thursday, Friday, Saturday = 10, Sunday } class ConsoleApp { static void Main(string[] args) { // Explicitly converting the enum elements to 'int' and then outputting them Console.WriteLine((int)Days.Monday); Console.WriteLine((int)Days.Tuesday); Console.WriteLine((int)Days.Wednesday); Console.WriteLine((int)Days.Thursday); Console.WriteLine((int)Days.Friday); Console.WriteLine((int)Days.Saturday); Console.WriteLine((int)Days.Sunday); } }
1. What is an enum in C#?
2. Which keyword is used for defining an enum?
3. In an enum, what is the default underlying type for the values?
What is an enum in C#?

What is an enum in C#?

Select the correct answer

Which keyword is used for defining an enum?

Which keyword is used for defining an enum?

Select the correct answer

In an enum, what is the default underlying type for the values?

In an enum, what is the default underlying type for the values?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 10
some-alt