C-SHARP - 3.14 Enum
In this video we introduce enumerations in C#. An enum is a structure that groups several named constants together to make code easier to read. Suppose we want to represent the days of the week: declaring seven separate const int values quickly becomes verbose. An enumeration solves this in a single, compact declaration. By default the first constant equals 0 and the underlying type is int, but you can specify another integral type such as short or byte if needed.
To create an enumeration, we type enum outside the Main method but inside the Program class, give it a name (for example JoursSemaine) and list the constants between braces. We can force a starting value by assigning the first constant explicitly, and every following constant is incremented automatically. It is also possible to give the same value to several constants, which is useful for grouping things such as error levels of equal severity.
enum JoursSemaine
{
Lundi = 1,
Mardi,
Mercredi,
Jeudi,
Vendredi,
Samedi,
Dimanche
}
Using the enum
- Declare a variable:
JoursSemaine jour = JoursSemaine.Lundi;. - Display the name with
Console.WriteLine(jour);which printsLundi. - Cast to its underlying value with
(int)jour; forSamediwe obtain6.
Inside the Main method, declare a variable of type JoursSemaine, assign it one of the constants of the enumeration and display it. Running the program prints the constant name. To obtain its numeric value you add (int) in front of the variable inside the Console.WriteLine call. We will explore enumerations more deeply in later chapters; the next short video proposes an exercise to practise this new concept.
Summary
Enumerations in C# provide a way to group related constants together, making code more readable and maintainable. They allow you to define a set of named constants (like days of the week) without having to create individual variable declarations. By default, enumeration values start at 0 and are of type `int`, though you can specify other types like `short` or customize the starting values as needed.
Key points
- Enumerations group multiple related constants to improve code readability and reduce clutter in your code.
- Enumerations are defined outside the Main method but within the program class using the `enum` keyword followed by the enum name and opening braces.
- Enum values start at 0 by default and are of type `int`, but the type can be specified (int, short, etc.).
- You can customize the starting value of the first constant (e.g., `Monday = 1`) and subsequent values increment automatically from there.
- Multiple constants can be assigned the same value, which is useful for grouping items with the same priority or severity level (e.g., error classifications).
- To retrieve the numeric value of an enum constant, cast it to `int` using the syntax `(int)variable`.
FAQ
What is the main benefit of using enumerations?
Enumerations allow you to group related constants together, making your code more organized and readable. Instead of creating individual constants for each value, you can define them all within a single enum definition.
Can I change the starting value of an enumeration?
Yes. You can assign a specific value to the first constant (e.g., `Monday = 1`), and the subsequent constants will automatically increment from that value.
How do I retrieve the numeric value of an enum constant?
You can cast the enum value to `int` using the syntax `(int)enumVariable` to retrieve its underlying numeric value.