#465 – Dumping All Names and Values for an Enumerated Type

You can use the Enum.GetValues method to iterate through all values of an enumerated type.  You can use this method to dump out the name and value of each enumeration value in the enumerated type.

        public enum Moods
        {
            NOMOOD = 0,
            Ambivalent = 1,
            Crabby = 10,
            Grouchy = Crabby - 1,
            Happy = 42,
            SuperHappy = 2 * Happy
        }
foreach (Moods mood in Enum.GetValues(typeof(Moods)))
    Console.WriteLine("{0} - {1}", mood, (int)mood);

Advertisement