C# Enumerate an Enum

In this short example, I will show you how to Enumerate an Enum

C-Sharp Enumerate An EnumSometimes we need to loop over an Enum; In this short post, I will show you have to loop over an Enum and display all the values.

The code was written in a C# console application using Visual Studio 2015

I hope you find this short post helpful.

Code Block

using System;
namespace Example
{
    internal static class Program
    {
        private static void Main(string[] args)
        {
            EnumerateOverDogs();
        }
        private enum Dogs
        {
            GermanShepherd,
            GreatPyrenees,
            IrishSetter,
            Keeshond
        }
        private static void EnumerateOverDogs()
        {
            foreach (Dogs dog in Enum.GetValues(typeof(Dogs)))
            {
                Console.WriteLine(dog);
            }
            Console.Read();
        }
    }
}

Summary

I hope you found this short blog helpful.