I would like to iterate over the values of a enum property, the following method works but it feels long winded. Are there any methods built in to the .net libraries or some for loop
I'm missing?
[Flags]
public enum Animals
{
Dog = 1,
Cat = 2,
Sheep = 4,
Cow = 8,
Goat = 16
}
var myAnimals = Animals.Dog | Animals.Sheep | Animals.Goat;
foreach (Animals animal in Enum.GetValues(typeof(Animals)))
{
if (myAnimals.HasFlag(animal))
{
Console.WriteLine($"You have a {animal}");
}
}
Syntactically this would be perfect.
foreach (Animals animal in myAnimals)
{
Console.WriteLine($"You have a {animal}");
}
Sure I could create a utility method, but if I am missing a builtin method I would prefer to use that.