- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to loop through all values of an enum in C#?
To loop through all the values of enum, use the Enum.GetValues().
Firstly, set an Enum −
public enum Grade { A, B, C, D, E, F };
Now, with foreach loop, you need to loop through Enum.GetValues(typeof(Grade)) −
foreach (Grade g in Enum.GetValues(typeof(Grade))) { Console.WriteLine(g); }
Here is the complete code −
Example
using System; public class EnumExample { public enum Grade { A, B, C, D, E, F }; public static void Main() { foreach (Grade g in Enum.GetValues(typeof(Grade))) { Console.WriteLine(g); } } }
Output
A B C D E F
- Related Articles
- How to loop through all the elements of an array in C#?
- How to iterate the values of an enum using a for loop in Java?
- How to loop through an array in Java?
- How to iterate the values in an enum in Java?
- How can I loop through all rows of a table in MySQL?
- How to loop through all the iframes elements of a page using jQuery?
- How do we use foreach statement to loop through the elements of an array in C#?
- Loop through an array in Java
- How to use for each loop through an array in Java?
- How to use for...in statement to loop through an Array in JavaScript?
- Loop through all MongoDB collections and execute query?
- What are enumerations in Java? How to retrieve values from an enum?
- Loop through an ArrayList using an Iterator in Java
- Split a string and loop through values in MySQL Procedure?
- How do you loop through a C# array?

Advertisements