Dynamic enumeration of Enum in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace w3mentorConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (var car in Enum.GetValues(typeof(Cars)))
            {
                Console.WriteLine(Enum.GetName(typeof(Cars), car));
            }
            Console.Read();
        }
        enum Cars
        {
            Ferrari, Polo, GMC, FordExplorer
        }
    }
}

Output:

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.