How to loop through all enum values in C#?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for How to loop through all enum values in C#?

πŸ“– Tech Blog: Looping Through All Enum Values in C#

πŸ‘‹ Hey there, fellow coders! Today, we're going to tackle a common question that often arises when we deal with enums in C#: "How do I loop through all the values of an enum?" πŸ”„

πŸ€” You might have come across this question while working on your latest C# adventure. Don't worry, we've got you covered! Let's dive right in and learn some easy-peasy ways to loop through all enum values in C#. πŸ’ͺ

πŸ’‘ Understanding the Problem

First things first, let's set the stage. Imagine you have the following enum called Foos:

public enum Foos
{
    A,
    B,
    C
}

You want to iterate through all the possible values of Foos using a loop. But how do we go about it? Let's walk through a couple of solutions step by step. πŸšΆβ€β™‚οΈ

πŸ” Solution 1: Using Enum.GetValues() Method

One straightforward way to achieve this is by using the Enum.GetValues() method. This method returns an array containing all the values of the specified enum type.

So, our loop would look something like this:

foreach (Foos foo in Enum.GetValues(typeof(Foos)))
{
    // Do something with each enum value
    Console.WriteLine(foo);
}

By calling Enum.GetValues(typeof(Foos)), we get an array of Foos values, which we can then iterate over in the foreach loop. Each enum value is stored in the foo variable, allowing us to perform actions on it.

πŸ” Solution 2: Using Enum.GetNames() Method

Another way to loop through the enum values is by using the Enum.GetNames() method. This method returns an array of the names of the constants in the enum type.

Here's an example of how to use it:

foreach (string fooName in Enum.GetNames(typeof(Foos)))
{
    Foos foo = (Foos)Enum.Parse(typeof(Foos), fooName);
    // Do something with each enum value
    Console.WriteLine(foo);
}

In this approach, we obtain an array of strings containing the names of the enum constants. Then, for each name, we convert it back to its respective enum value using Enum.Parse(). Now, we can utilize the foo variable to manipulate each enum value within the loop.

πŸ“£ Taking Action

And there you have it, two simple and effective ways to loop through all enum values in C#! 😎 Now, you can confidently handle enum iterations in your projects and make your code even more fantastic! πŸš€

Do you have any other nifty tricks up your sleeve for working with enums? Share your wisdom in the comments section below! πŸ—£οΈ Let's help each other grow as developers and create amazing things together! πŸ’«

πŸ”— Further Reading

If you're interested in exploring this topic further, you can check out the following resources:

That's all for now, folks! Happy coding! πŸ’»βœ¨

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

πŸ”₯ πŸ’» πŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! πŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings πŸ’₯βœ‚οΈ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide πŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? πŸ€” Well, my