Is this GetEnumAsStrings<T>() method reinventing the wheel?
- by Edward Tanguay
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
}
}