Freephone Panwal Freephone Panwal - 16 days ago 6
C# Question

Converting enum values into an string array

public enum VehicleData
{
Dodge = 15001,
BMW = 15002,
Toyota = 15003
}


I want to get above values 15001, 15002, 15003 in string array as shown below:

string[] arr = { "15001", "15002", "15003" };


I tried below command but that gave me array of names instead of values.

string[] aaa = (string[]) Enum.GetNames(typeof(VehicleData));


I also tried
string[] aaa = (string[]) Enum.GetValues(typeof(VehicleData));
but that didn't work too.

Any suggestions?

Answer

Use GetValues

Enum.GetValues(typeof(VehicleData))
    .Cast<int>()
    .Select(x => x.ToString())
    .ToArray();

Live demo