Is this GetEnumAsStrings<T>() method reinventing the wheel?

Posted by Edward Tanguay on Stack Overflow See other posts from Stack Overflow or by Edward Tanguay
Published on 2010-06-10T14:15:54Z Indexed on 2010/06/10 14:22 UTC
Read the original article Hit count: 328

Filed under:
|
|
|
|

I have a number of enums and need to get them as List<string> objects in order to enumerate through them and hence made the GetEnumAsStrings<T>() method.

But it seems to me there would be an easier way.

Is there not a built-in method to get an enum like this into a List<string>?

using System;
using System.Collections.Generic;

namespace TestEnumForeach2312
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> testModes = StringHelpers.GetEnumAsStrings<TestModes>();
            testModes.ForEach(s => Console.WriteLine(s));

            Console.ReadLine();
        }
    }

    public static class StringHelpers
    {
        public static List<string> GetEnumAsStrings<T>()
        {
            List<string> enumNames = new List<string>();
            foreach (T item in Enum.GetValues(typeof(TestModes)))
            {
                enumNames.Add(item.ToString());
            }
            return enumNames;
        }
    }

    public enum TestModes
    {
        Test,
        Show,
        Wait,
        Stop
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about list