Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 871491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:38:53+00:00 2026-05-15T10:38:53+00:00

I have a number of enums and need to get them as List<string> objects

  • 0

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
    }
}

Addendum:

Thanks everyone, very insightful. Since I ultimately needed this for Silverlight which doesn’t seem to have GetValues() or GetNames() for enums, I made this method which I created from this method:

public static List<string> ConvertEnumToListOfStrings<T>()
{
    Type enumType = typeof(T);
    List<string> strings = new List<string>();
    var fields = from field in enumType.GetFields()
                 where field.IsLiteral
                 select field;
    foreach (FieldInfo field in fields)
    {
        object value = field.GetValue(enumType);
        strings.Add(((T)value).ToString());
    }
    return strings;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:38:53+00:00Added an answer on May 15, 2026 at 10:38 am

    You could do it as a one-liner using LINQ:

    var enums = Enum.GetNames(typeof(TestModes)).ToList();
    

    Now, keep in mind that GetNames returns an array of strings… so you might not even need ToList().

    Edit:
    There are ways to improve on your edited code. Here’s a simple one that uses ToList rather than explicitly instantiating the list:

    public static List<string> ConvertEnumToListOfStrings<T>()
    {
        Type enumType = typeof(T);
        var fields = from field in enumType.GetFields()
                     where field.IsLiteral
                     select ((T)field.GetValue(enumType)).ToString();
        return fields.ToList();    
    }
    

    And this next one is my personal preference. Why instantiate a list at all? You probably just need to iterate over the names, not add or remove them from a list. So just use IEnumerable and don’t bother building the list at all. Saves you one (admittedly small) iteration and the memory overhead of another object.

    public static IEnumerable<string> GetEnumNames<T>()
    {
        Type enumType = typeof(T);
        var fields = from field in enumType.GetFields()
                     where field.IsLiteral
                     select ((T)field.GetValue(enumType)).ToString();
        return fields;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class Errors. I have value errorCode. I need get string by errorCode.
I have a number of enums in my application which are used as property
I have a number of RGBA pixels, each of them has an alpha component.
Have enum with inner fields, kind of map. Now I need to get enum
I have a number of Data Transfer Objects (DTO's) that map onto data structures
I need to generate a unique integer id for a string. Reason: I have
I have a number of request types - which really are enums. But in
I have a list of enums as follows: public enum EventID : uint {
I have a number of columns that only need to store a few values
I'm working on a project were we have number (5 at the moment) of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.