How to enumerate all values of an enum in C#?
Hello colleagues! I'm a beginner C# developer and I've encountered a task that seems simple but has stumped me.\n\nI have an enum for days of the week:\n\n```csharp\npublic enum DaysOfWeek\n{\n Monday,\n Tuesday,\n Wednesday,\n Thursday,\n Friday,\n Saturday,\n Sunday\n}\n```\n\nI need to output all possible values of this enum to the console, i.e., get a list: Monday, Tuesday, Wednesday, and so on. I tried using a `foreach` loop, but the compiler gives an error. Here's my code:\n\n```csharp\nforeach (var day in DaysOfWeek)\n{\n Console.WriteLine(day);\n}\n```\n\nThe error says that `DaysOfWeek` is not a collection or an array. I understand that an enum is a value type, not a collection, but how do I get all its values as a list?
No answers yet.
Log in to leave an answer