How to enumerate an enum?
How to enumerate an enum?
I completed my post-graduation in 2013 in the engineering field. Engineering is the application of science and math to solve problems. Engineers figure out how things work and find practical uses for scientific discoveries. Scientists and inventors often get the credit for innovations that advance the human condition, but it is engineers who are instrumental in making those innovations available to the world. I love pet animals such as dogs, cats, etc.
Aryan Kumar
13-Jul-2023There are two ways to enumerate an enum in C#:
C#
This code first creates an enum called
Colors. TheColorsenum has three values:Red,Green, andBlue.The code then uses a foreach loop to enumerate the
Colorsenum. The foreach loop will iterate through each value of theColorsenum and print the value to the console.Enum.GetValues()method:C#
This code first creates an enum called
Colors. TheColorsenum has three values:Red,Green, andBlue.The code then uses the
Enum.GetValues()method to get an array of all the values of theColorsenum. TheEnum.GetValues()method takes the type of the enum as a parameter.The code then iterates through the array of values and prints each value to the console.