0Pricing
C# Academy · Lesson

Iterating Enum Values

Enumerate all members with Enum.GetValues.

Iterating Enum Values is a free C# Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Listing Enum Members

Sometimes you need every value of an enum, for example to build a menu. Enum.GetValues provides them.

using System;

enum Day { Mon, Tue, Wed }
class Program {
    static void Main() {
        foreach (Day d in Enum.GetValues<Day>())
            Console.WriteLine(d);
    }
}

Generic GetValues

The generic Enum.GetValues<T>() returns a strongly typed array you can loop over directly.

using System;

enum Color { Red, Green, Blue }
class Program {
    static void Main() {
        Color[] all = Enum.GetValues<Color>();
        Console.WriteLine(all.Length);
    }
}

Getting Member Names

Enum.GetNames<T>() returns the member names as strings.

using System;

enum Color { Red, Green, Blue }
class Program {
    static void Main() {
        foreach (string name in Enum.GetNames<Color>())
            Console.WriteLine(name);
    }
}

Names and Values Together

Loop the values and cast to int to print both the name and the underlying number.

using System;

enum Color { Red = 1, Green = 2, Blue = 4 }
class Program {
    static void Main() {
        foreach (Color c in Enum.GetValues<Color>())
            Console.WriteLine(c + " = " + (int)c);
    }
}

Counting Members

The length of the values array tells you how many members the enum has.

using System;

enum Suit { Hearts, Diamonds, Clubs, Spades }
class Program {
    static void Main() {
        int count = Enum.GetValues<Suit>().Length;
        Console.WriteLine(count);
    }
}

Building a Menu

Iterating values is perfect for printing a numbered selection list.

using System;

enum Option { Start, Settings, Quit }
class Program {
    static void Main() {
        int i = 1;
        foreach (Option o in Enum.GetValues<Option>())
            Console.WriteLine(i++ + ") " + o);
    }
}

Filtering While Iterating

You can skip certain members, such as a None sentinel, while looping.

using System;

enum State { None, Active, Closed }
class Program {
    static void Main() {
        foreach (State s in Enum.GetValues<State>())
            if (s != State.None)
                Console.WriteLine(s);
    }
}

Iterating with LINQ

Combine Enum.GetValues with LINQ for filtering or transformation.

using System;
using System.Linq;

enum Level { Low = 1, Medium = 2, High = 3 }
class Program {
    static void Main() {
        var highOnes = Enum.GetValues<Level>().Where(l => (int)l >= 2);
        foreach (var l in highOnes) Console.WriteLine(l);
    }
}

Older Non-Generic Form

Before generics, you used Enum.GetValues(typeof(T)), which returns an untyped array.

using System;

enum Color { Red, Green, Blue }
class Program {
    static void Main() {
        foreach (Color c in Enum.GetValues(typeof(Color)))
            Console.WriteLine(c);
    }
}

Flags and Iteration

For flags enums, iterating lists each individual flag; combined convenience members appear too, so filter if needed.

using System;

[Flags]
enum Perm { Read = 1, Write = 2, Execute = 4 }
class Program {
    static void Main() {
        foreach (Perm p in Enum.GetValues<Perm>())
            Console.WriteLine(p + " = " + (int)p);
    }
}

Why Iterate Enums

Iterating keeps UI and validation in sync with the enum: add a member and loops automatically include it.

using System;

enum Size { Small, Medium, Large }
class Program {
    static void Main() {
        foreach (Size s in Enum.GetValues<Size>())
            Console.WriteLine("Available: " + s);
    }
}

Quick Check

Test your understanding of iterating enums.

Recap

Enum.GetValues<T>() returns every member as a typed array, and Enum.GetNames<T>() returns their names. You can loop to print names with their numeric values, count members, build menus, or filter with LINQ.

Iterating keeps menus and validation automatically in sync as the enum grows.

Frequently asked questions

Is the “Iterating Enum Values” lesson free?

Yes — the full text of “Iterating Enum Values” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Iterating Enum Values”?

Enumerate all members with Enum.GetValues. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Iterating Enum Values” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining and Using Enums
  2. Flags Enums and Bitwise Combinations
  3. Enum Conversions and Parsing
  4. Iterating Enum Values
← Back to C# Academy