Freephone Panwal Freephone Panwal - 1 year ago 98
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 Source

Use GetValues

    .Select(x => x.ToString())

Live demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download