14

Let's say I have a C# enum called MyEnum:

public enum MyEnum
{
    Apple,
    Banana,
    Carrot,
    Donut
}

And I have a List<MyEnum> such as:

List<MyEnum> myList = new List<MyEnum>();
myList.Add(MyEnum.Apple);
myList.Add(MyEnum.Carrot);

What is the easiest way to convert my List<MyEnum> to a List<string>? Do I have to create a new List<string> and then iterate through the enum list, one item at a time, converting each enum to a string and adding it to my new List<string>?

2
  • 3
    var myNewList = myList.Select(t => t.ToString()).ToList(); Commented Jun 1, 2018 at 0:11
  • This did the trick. Thank you! Commented Jun 1, 2018 at 0:19

2 Answers 2

16

Since you are using a List, the easiest solution would be to use the ConvertAll method to obtain a new List containing string representations. Here's an example:

List<string> stringList = myList.ConvertAll(f => f.ToString());

There are other ways to accomplish this, but this way will get the job done and uses syntax that should be in whatever version of .NET you're using because it's been around for a long time.

Sign up to request clarification or add additional context in comments.

Comments

0
var list= (from action in (MyEnum[]) Enum.GetValues(typeof(MyEnum)) select action.ToString()).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.