C# enum Usage and Examples

The enumeration type declaration specifies a name for a collection of linked symbolic constants. Enumeration is used in “multiple choice” scenarios, in which a decision is taken from a pre-defined number of “choices” that have been established at compile time.

Enumeration classes aid in overcoming the constraints of Enum types. Enumeration classes are not a novel notion. There is a wealth of information available on how to utilize Enumeration classes in Domain-Driven Design (DDD).

Enumeration types (also known as enumerations) are a quick and easy way to establish a group of named integer constants that may be assigned to variables. Assume you need to define a variable whose value indicates the day of the week. Only seven relevant values can be stored in this variable. Enumerated types can be used to define these values. The enum keyword is used to declare enumeration types.

C# enumerations type declaration syntax

The syntax for defining enum in C # is as follows.

C# enum

In C#, the enum keyword is used to define new enumeration types. An implementation of an enum declaration is shown below.

enum declaration

The “Days” enumeration in the above code sample specifies a list of defined constants on every line, separated by commas. By design, the enum’s first entry begins with the integer value 0. The second element has a value of 1, and so on.

The basic type of each entry in the enumeration is int by default. A colon can be used to identify another integer value type. If you do not supply a value for any of the items in the enumerator list, their value will rise in increments of one. In the preceding example, Days.Sunday has a value of 0, Days.Monday is number one, and so forth.

If you don’t explicitly set a value to a new Days object, it will have the default value Days. Sunday (0). When establishing an enumeration, pick the most logical default value and assign it a value of zero.

As a result, unless an enumeration is explicitly provided a value when it is formed, all enumerations derived will have the default value. Although the enumeration is case-sensitive, it is not advised.

It should be noted that the System.Enum type is the abstract base class of every enumeration type (it is a separate type distinct from the enumeration type’s base type), and the members are inherited from System.

Enum, which can be found in any enumeration type. Any enumerated type may be boxed to System.Enum, and System.Enum can be unboxed to any enumerated type. System.

Enum is not an enumeration type in and of itself. On the flip side, it is a class type from which all enumeration types are generated. The type System. Enum is descended from the type System.ValueType, which is descended from the type object. The value of type System. Enum can be null or a reference to a boxed item of any enumerated type at runtime.

Benefits of Using C# Enumeration

  1. Enums in C# are highly typed constants that help the user interpret the code.
  2. An enum is an excellent approach to associate a set of constant integral values with a single variable.
  3. Since they are value types and are stored on the stack, enums take considerably less space in C#.
  4. Enum simply allows you to gather together a collection of related constants which are more understandable than magic numbers. Instead of integer numbers, we must know the names of real constants.
  5. Enum can be defined within or outside of a class or struct in C#.
  6. Using bitwise operators, we may assign different values to enum objects via the Flags attribute.

C# Enum Examples

Accessing an Enum

To access enumerations, use the dot (.) syntax: enum.member

Accessing an Enum

Converting a string to an enum in C#

To convert a string to an enum, use the static function Enum.Parse. The first parameter of this function is an enum type, the second is a string type, and the third is a boolean value that may be used to disregard the case.

To further understand, consider the following functioning example.

Converting a string to an enum in C#

Converting an Enum to string in C#

To parse an enum to a string, use the ToString() function.

To further understand, consider the following functioning example.

Converting an Enum to string in C#

C# Enums’ Flag Attribute

In enum, the Flags attribute is often used to indicate a set of alternative values instead of a single value.

When we wish to assign various values to an enum, we may utilize the [Flag] attribute. Such collections are mostly used, for example, with the switch & bitwise (OR |) operators.

C# Enums' Flag Attribute

In C#, how do you iterate across an enumeration (Enum)?

The enum type is among the most often utilized features in practically every implementation. While the declarations and applications appear to be simple, there are a few things we should be explicit about, and this is one of them.

How to cycle over an Enum in C#? or can you loop through with all the enum values in C#? – this is another often asked topic in a novice interview. There are various ways to accomplish this.

To answer this, the Enum class serves as the basic class for enumerations and Enum. GetNames() is a method that retrieves an array of names and returns a string array of the names.

Let’s look at a basic example to help us comprehend. Consider the following Enumeration:

How to cycle over an Enum in C#

You can now loop through the enumeration using Enum.GetNames() as seen below. Enum.GetNames() requires enum types as arguments, which may be provided using the typeof keyword. This will return an array containing the names of the WorkingDays enumeration’s constants.

Enum.GetNames()

When you execute this, you will get the output shown below.

Enum can also be used in another method. Iterate through the entries using GetValues().

GetValues()

This will also yield the same result.

Leave a Comment