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>
?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With