C# Enum Examples – Programming, Pseudocode Example, C# Programming Example
C# C# Console

C# Enum Examples

An enumeration, or enumerator list, declares a series of related constants, each with an integer value. Enumerations are useful for defining sequences and states, particularly when there is a natural progression through those states. This is because each constant in the list can be formatted and compared using either its name or value.

An enum is a special “class” that represents a group of constants (unchangeable/read-only variables).

To create an enum, use the enum keyword (instead of class or interface), and separate the enum items with a comma:

 

Why And When To Use Enums?

Use enums when you have values that you know aren’t going to change, like month days, days, colors, deck of cards, etc.

 

Advantages of using Enums

The advantage of the enum is to make the function easy to change the values for the future. It also reduces the error which are occured by the mistyping numbers.

Enums are not allocated in the memory they exist only in the compilation stage. Enum.parse throws an error if a value is incorrect. So we use Enum.IsDefined to check. So every helper method helps a enum a lot.

 

Example 1:

Output:

Medium

 

Example 2:

Output:

3

Example 3:

Output:

Sun

 

Example 4:

Output:

Medium level

 

Example 5:

Output:

Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
12

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.