Avatar ·

How to iterate over all values of an enum in C#?

Hello everyone! I'm new to C# and encountered this task: I have an enum for days of the week: ```csharp public enum DaysOfWeek { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } ``` I need to display all days of the week on the screen or, for example, fill a ComboBox in WinForms. Currently I'm doing it manually, but it's inconvenient and inflexible — if I add a new day, I'll have to fix the code everywhere. I tried writing a simple loop, but the compiler complains that `DaysOfWeek` cannot be used as a collection: ```csharp foreach (var day in DaysOfWeek) // Error: foreach cannot work with type DaysOfWeek { Console.WriteLine(day); } ``` Please advise how to correctly iterate over all enum values.