This question already has an answer here:
How do I enumerate an enum in C#? 26 answers
public enum Foos
{
A,
B,
C
}
Is there a way to loop through the possible values of Foos
?
Basically?
foreach(Foo in Foos)
This question already has an answer here:
How do I enumerate an enum in C#? 26 answers
public enum Foos
{
A,
B,
C
}
Is there a way to loop through the possible values of Foos
?
Basically?
foreach(Foo in Foos)
Yes you can use the GetValues
method:
var values = Enum.GetValues(typeof(Foos));
Or the typed version:
var values = Enum.GetValues(typeof(Foos)).Cast<Foos>();
I long ago added a helper function to my private library for just such an occasion:
public static class EnumUtil {
public static IEnumerable<T> GetValues<T>() {
return Enum.GetValues(typeof(T)).Cast<T>();
}
}
Usage:
var values = EnumUtil.GetValues<Foos>();
.Cast<Foos>
), and (2) you don't need to box all the values and unbox them again. Şafak's cast will remain valid as long as they don't change the array type returned to some other type (like object[]
). But we can be completely sure they won't because (a) it would lose performance, (b) there are already millions of codelines using Şafak's cast, and they would all break with a runtime exception. –
Dibbell public static IReadOnlyList<T> GetValues<T>() { return (T[])Enum.GetValues(typeof(T)); }
. But yeah, performance difference is negligible in common usage. I just don't like the idea of creating an iterator when I already have an iterable (enumerable) object to return. –
Imperceptible foreach (var v in values)
–
Clay (Foos[])
, my code run 1,6 times faster. Try it yourself. I tested an empty foreach with StopWatch. for (int i = 0; i < 1000000; i++) foreach (Foos f in (Foos[])Enum.GetValues(typeof(Foos))) {}
–
Compute HashSet
. "Microsoft won't change it because we're all already misusing it" is a poor excuse. en.wikipedia.org/wiki/Tragedy_of_the_commons –
Heredes where T : Enum
–
Ommatidium foreach(Foos foo in Enum.GetValues(typeof(Foos)))
Foos
nothing is magically inferred. It is an explicit cast. –
Snivel foreach (EMyEnum val in Enum.GetValues(typeof(EMyEnum)))
{
Console.WriteLine(val);
}
Credit to Jon Skeet here: http://bytes.com/groups/net-c/266447-how-loop-each-items-enum
foreach (Foos foo in Enum.GetValues(typeof(Foos)))
{
...
}
UPDATED
Some time on, I see a comment that brings me back to my old answer, and I think I'd do it differently now. These days I'd write:
private static IEnumerable<T> GetEnumValues<T>()
{
// Can't use type constraints on value types, so have to do check like this
if (typeof(T).BaseType != typeof(Enum))
{
throw new ArgumentException("T must be of type System.Enum");
}
return Enum.GetValues(typeof(T)).Cast<T>();
}
You can cast the array directly: (T[])Enum.GetValues(typeof(T))
@SafakGür, this version has less overhead IMO. –
Dday Constraint cannot be special class 'Enum'
–
Limelight Enum
(as well as unmanaged
and delegate
) as generic constraints. –
Baudin static void Main(string[] args)
{
foreach (int value in Enum.GetValues(typeof(DaysOfWeek)))
{
Console.WriteLine(((DaysOfWeek)value).ToString());
}
foreach (string value in Enum.GetNames(typeof(DaysOfWeek)))
{
Console.WriteLine(value);
}
Console.ReadLine();
}
public enum DaysOfWeek
{
monday,
tuesday,
wednesday
}
Yes. Use GetValues()
method in System.Enum
class.
© 2022 - 2024 — McMap. All rights reserved.
(T[])Enum.GetValues(typeof(T))
– Imperceptible