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

Зміст курсу

C# Beyond Basics

EnumeratorsEnumerators

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.cs

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.cs

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

For-example:

cs

index.cs

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.cs

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

cs

index.cs

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#?

Виберіть правильну відповідь

Which keyword is used for defining an enum?

Виберіть правильну відповідь

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

Виберіть правильну відповідь

Все було зрозуміло?

Секція 2. Розділ 10
course content

Зміст курсу

C# Beyond Basics

EnumeratorsEnumerators

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.cs

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.cs

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

For-example:

cs

index.cs

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.cs

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

cs

index.cs

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#?

Виберіть правильну відповідь

Which keyword is used for defining an enum?

Виберіть правильну відповідь

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

Виберіть правильну відповідь

Все було зрозуміло?

Секція 2. Розділ 10
some-alt