Reputation: 494
I have an Enum as below:
enum Days
{
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
I want to get enum constant values as list of integers. I did it in following way
List<int> dayCodesList = Enum.GetValues(typeof(Days)).Cast<Days>().Select(d => Convert.ToInt32(d)).ToList();
Is there any other more efficient alternate way to achieve it?
Upvotes: 1
Views: 2625
Reputation: 8004
I want to get enum constant values as list of integers. I did it in following way
List<int> dayCodesList = Enum.GetValues(typeof(Days)).Cast<Days>().Select(d => Convert.ToInt32(d)).ToList();
Is there any other more efficient alternate way to achieve it?
Depending on the issues, if any, could be contingent on performance and or code adeptness. Without elucidation it’s harder to address your concern. I am going to assume you want something shorter than what you’ve tried and or confirmation that you’ve properly adapted what you need.
The code below is indeed giving you what you want, a List<int>
with the default enum value. The issue is you have an extra Select
which you don't need; the Cast
will explicitly inform the compiler that you intend to make the conversion yourself.
List<int> dayCodesList = Enum.GetValues(typeof(Days)).Cast<Days>().Select(d => Convert.ToInt32(d)).ToList();
You can remove that Select
altogether and now be left with see Sangeeth Answer:
List<int> dayCodesList = Enum.GetValues(typeof(Days)).Cast<int>().ToList();
Another option is to handle getting the values yourself. Below I've done this with a static class that utilizes an extension.
using System;
using System.Collections.Generic;
using System.Reflection;
public static class EnumHelper
{
public static IEnumerable<T> GetAllValues<T>(this Type @enum)
{
if ([email protected])
throw new ArgumentException("Type must be an enum");
FieldInfo[] flds = @enum.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
foreach (FieldInfo fInfo in flds)
{
yield return (T)Convert.ChangeType(fInfo.GetRawConstantValue(), typeof(T));
}
}
}
In the above, I am using Reflection
to get all the fields associated with the passed in enum. For each field I grab the RawConstantValue
and convert it to the type requested.
To implement the above, you can do something like:
var dayCodes = typeof(Days).GetAllValues<int>();
This will return an IEnumerable<int>
, some benefits to this. Since it returns an IEnumerable
it defers execution until it's needed; it's also not contained to one type either. For example you could do:
var dayCodesList = typeof(Days).GetAllValues<int>().ToList(); // returns a List<int>
var dayCodesArray = typeof(Days).GetAllValues<int>().ToArray(); // returns an array of ints
var dayCodesListString = typeof(Days).GetAllValues<string>().ToList(); // returns a List<string>
While I was at it, I decided to do some benchmarks on the different implementations through out this post, please see them below.
Original Implementation (OP)
Method | Mean | Error | StdDev |
---|---|---|---|
OriginalSolution | 2.011 μs | 0.0372 μs | 0.0690 μs |
Sangeeth Implementation
Method | Mean | Error | StdDev |
---|---|---|---|
SangeethSolution | 1.655 μs | 0.0330 μs | 0.0682 μs |
Codexer Implementation
Method | Mean | Error | StdDev |
---|---|---|---|
CodexerSolution | 9.876 ns | 0.2718 ns | 0.7799 ns |
You can see a great difference between all three implementations.
Upvotes: -1
Reputation: 9
You can also use List dayCodesList = new List(); Array.ForEach(Enum.GetValues(),value => dayCodesList.Add((int)value));
In .net# 5.0 You can use GetValues it gets you Days[]. var dayCodesList = Enum.GetValues().Select(d => Convert.ToInt32(d)).ToList(); Or Array.ConvertAll(Enum.GetValues(), value =>(int)value).ToList();
Upvotes: -1
Reputation: 1512
This will get you list of integers from your Enum
var listOfIntegers = Enum.GetValues(typeof(Days)).Cast<int>().ToList();
Upvotes: 10