Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge
  • All
  • C#
  • MVC
  • Web API
  • Azure
  • IIS
  • JavaScript
  • Angular
  • Node.js
  • Java
  • Python
  • SQL Server
  • SEO
  • Entrepreneur
  • Productivity

How to Loop Through an Enum in C#?

Here you will learn how to enumerate or loop through an enum.

In C#, an enum is used to assign constant names to a group of numeric integer values. It makes constant values more readable, for example, WeekDays.Monday is more readable than number 0 when referring to the day in a week.

An enum can be looped through using Enum.GetNames<TEnum>(), Enum.GetNames(), Enum.GetValues<TEnum>(), or Enum.GetValues() static methods with the foreach loop.

The following example gets the names of an enum using the Enum.GetNames<TEnum>() method.

Example: Loop through Enum Member Names in .NET 4.x
public enum SocialNetworks { Facebook, Linkedin, Twitter, Instagram };

class Program
{
    static void Main(string[] args)
    {
        foreach (var name in Enum.GetNames(typeof(SocialNetworks)))
        {
            Console.WriteLine(name);
        }
    }
}
Try it
Example: Loop through Enum Member Names in .NET 6
public enum SocialNetworks { Facebook, Linkedin, Twitter, Instagram };

class Program
{
    static void Main(string[] args)
    {
        foreach (var name in Enum.GetNames<SocialNetworks>())
        {
            Console.WriteLine(name);
        }
    }
}
Try it
Output:
Facebook Linkedin Twitter Instagram

The Enum.GetValues<TEnum>() is a static method that retrieves an array of the constant values of the specified enum.

The following example shows how to get the values of an enum using the Enum.GetValues<TEnum>() method.

Example: Loop through Enum Values in .NET 4.x
public enum SocialNetworks {  Facebook = 3, Linkedin = 4, Twitter = 5, Instagram = 8};

class Program
{
    static void Main(string[] args)
    {
        foreach (var val in Enum.GetValues(typeof(SocialNetworks))
        {
            Console.WriteLine((int)val);
        }
    }
}
Try it
Example: Loop through Enum Values
public enum SocialNetworks {  Facebook = 3, Linkedin = 4, Twitter = 5, Instagram = 8};

class Program
{
    static void Main(string[] args)
    {
        foreach (var val in Enum.GetValues<SocialNetworks>())
        {
            Console.WriteLine((int)val);
        }
    }
}
Try it
Output:
0 1 2 3
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.