The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,510 other followers

.NET/C# – an easier foreach for enums using generic methods

Posted by jpluimers on 2009/04/28

I like enums. Maybe because of my Turbo Pascal and Delphi background. They are descriptive. Makes code easier to read.

        public enum TrafficLightColors
        {
            Red,
            Yellow,
            Green
        }

But using them in C# foreach loops is a bit of a pain, not so much in the loop itself, but more in getting the list of values to loop over.

The Delphi equivalent would be like this, including a loop to run through all of the enumeration values (note that in Delphi, it us good practice to start types with a T):

type
  TTrafficLightColors = (Red, Yellow, Green);
var
  TrafficLightColor: TTrafficLightColors;
begin
  for TrafficLightColor := Low(TTrafficLightColors) to High(TTrafficLightColors) do
    // some business logic
end;

Quite a while ago, I was fed up writing code to get the list of values like below; it felt much to awkward, and way too many occasions of the TrafficLightColors type, so this is an excellent candidate to simplify using use C# Generics.

            Array values = Enum.GetValues(typeof(TrafficLightColors));
            TrafficLightColors[] colorsArray = (TrafficLightColors[])values;
            foreach (TrafficLightColors color in colorsArray)
                Console.WriteLine(color);

I’d much rather write code like this using the TrafficLightColors type much less:

            TrafficLightColors[] colorsArray = EnumHelper.EnumToArray<TrafficLightColors>();
            foreach (TrafficLightColors color in colorsArray)
                Console.WriteLine(color);

            List<TrafficLightColors> colorsList = EnumHelper.EnumToList<TrafficLightColors>();
            foreach (TrafficLightColors color in colorsList)
                Console.WriteLine(color);

In fact, I’d even hoped that extension methods would eliminate the use of the reference to EnumHelper but explaining that will be part of a future blog.

So lets end with the tiny EnumHelper class:

using System;
using System.Collections.Generic;

namespace bo.Generic
{
    public class EnumHelper
    {
        public static T[] EnumToArray<T>()
        {
            Array values = Enum.GetValues(typeof(T));
            T[] result = (T[])values;
            return result;
        }

        public static List<T> EnumToList<T>()
        {
            T[] array = EnumToArray<T>();
            List<T> result = new List<T>(array);
            return result;
        }
    }
}

That should get you going simplifying your enum based foreach loops.

–jeroen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: