C-SHARP - 3.15 Enum (Demo)
We now practise enumerations in C# with a short exercise. Using the JoursSemaine enum (with constants starting at 1 and ending on Sunday with value 7), your program must display It is the weekend, we are when the chosen day is Saturday or Sunday, and It is a weekday, we are otherwise. The output must adapt automatically when you change the stored day.
Because Friday equals 5, every constant whose numeric value is strictly greater than 5 represents the weekend. To test this we need the integer value of the enumeration constant, which is obtained by casting with (int)jour as seen in the previous video. The condition therefore becomes if ((int)jour > 5).
Correction
- Use
(int)jourto compare with the numeric threshold. - Inside the
ifbranch (weekend), callstring.Format("It is the weekend, we are {0}", jour). - Inside the
elsebranch (week), concatenate directly with"It is a weekday, we are " + jour.
JoursSemaine jour = JoursSemaine.Samedi;
if ((int)jour > 5)
{
Console.WriteLine(string.Format("It is the weekend, we are {0}", jour));
}
else
{
Console.WriteLine("It is a weekday, we are " + jour);
}
Setting jour to Samedi shows the weekend message; with Dimanche the same branch is taken with the new day; with Mercredi we fall in the else and see the week message. We used two different display techniques on purpose: string.Format is recommended for clarity, while string concatenation works but is harder to visualise. That is all for this video on enumerations, see you in the next one.
Summary
This lesson demonstrates a practical implementation of C# enumerations through a real-world exercise. The program determines whether a given day is a weekend or weekday by checking if the enum value exceeds 5 (with Friday = 5, Saturday/Sunday > 5). The lesson covers enum value conversion to integers, conditional logic (if/else), and different display formatting methods using String.Format() and concatenation.
Key points
- Initialize enum values starting at 1 instead of 0 for cleaner comparison logic when distinguishing weekdays from weekends
- Cast enum values to int using (int) type conversion to perform numeric comparisons in conditional statements
- Use if/else statements to distinguish weekdays (values ≤5) from weekends (values >5) based on enum constant values
- Apply String.Format() for formatted output with placeholders ({0}) when displaying multiple values together
- Use enum.ToString() to retrieve the constant name (e.g., "Saturday") for human-readable day display in output
- Test the program with different day enum values to verify weekend/weekday detection logic works correctly
FAQ
Why initialize the enum starting at 1 instead of 0?
Starting at 1 makes the numeric comparison logic cleaner for determining weekends (Friday = 5, so >5 means weekend), avoiding off-by-one errors that would occur if starting from 0.
How do you retrieve the name of an enum constant for display?
Use the enum variable with .ToString() method (e.g., jour.ToString()) to get the constant name as a string, such as "Saturday" or "Monday".
When should you use String.Format() versus string concatenation for output?
String.Format() provides better readability and maintainability for complex strings with multiple values, while concatenation is simpler for straightforward displays. Both approaches work; choose based on your code clarity preference.