C# || How To Iterate & Get The Values Of An Enum Using C#
The following is a module with functions which demonstrates how to iterate and get the values of an enum using C#.
This function is a generic wrapper for the Enum.GetValues function.
1. Get Values – Basic Enum
The example below demonstrates the use of ‘Utils.Methods.GetEnumValues‘ to get the values of a simple Enum.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Get Values - Basic Enum enum Colors { Red, Green, Blue, Yellow } var colors = Utils.Methods.GetEnumValues<Colors>(); foreach (var color in colors) { Console.WriteLine($"Numeric Value: {(int)color}, Name: {color.ToString()}"); } // expected output: /* Numeric Value: 0, Name: Red Numeric Value: 1, Name: Green Numeric Value: 2, Name: Blue Numeric Value: 3, Name: Yellow */ |
2. Get Values – Numbered Enum
The example below demonstrates the use of ‘Utils.Methods.GetEnumValues‘ to get the values of a numbered Enum.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// Get Values - Numbered Enum public enum Pets { None = 0, Dog = 1, Cat = 2, Rodent = 4, Bird = 8, Fish = 16, Reptile = 32, Other = 64 } var pets = Utils.Methods.GetEnumValues<Pets>(); foreach (var pet in pets) { Console.WriteLine($"Numeric Value: {(int)pet}, Name: {pet.ToString()}"); } // expected output: /* Numeric Value: 0, Name: None Numeric Value: 1, Name: Dog Numeric Value: 2, Name: Cat Numeric Value: 4, Name: Rodent Numeric Value: 8, Name: Bird Numeric Value: 16, Name: Fish Numeric Value: 32, Name: Reptile Numeric Value: 64, Name: Other */ |
3. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 9, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.cs // Description: Handles general utility functions // ============================================================================ using System; using System.Collections.Generic; namespace Utils { public static class Methods { /// <summary> /// Returns a List of the values of the constants in a specified enumeration /// </summary> /// <returns>The values contained in the enumeration</returns> public static List<T> GetEnumValues<T>() { var type = typeof(T); if (!type.IsSubclassOf(typeof(System.Enum))) { throw new InvalidCastException($"Unable to cast '{type.FullName}' to System.Enum"); } var result = new List<T>(); foreach (T value in System.Enum.GetValues(type)) { result.Add(value); } return result; } } }// http://programmingnotes.org/ |
4. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 9, 2021 // Taken From: http://programmingnotes.org/ // File: Program.cs // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ using System; using System.Diagnostics; using System.Collections.Generic; public class Program { enum Colors { Red, Green, Blue, Yellow } public enum Pets { None = 0, Dog = 1, Cat = 2, Rodent = 4, Bird = 8, Fish = 16, Reptile = 32, Other = 64 } static void Main(string[] args) { try { var colors = Utils.Methods.GetEnumValues<Colors>(); foreach (var color in colors) { Display($"Numeric Value: {(int)color}, Name: {color.ToString()}"); } Display(""); var pets = Utils.Methods.GetEnumValues<Pets>(); foreach (var pet in pets) { Display($"Numeric Value: {(int)pet}, Name: {pet.ToString()}"); } } catch (Exception ex) { Display(ex.ToString()); } finally { Console.ReadLine(); } } static void Display(string message) { Console.WriteLine(message); Debug.Print(message); } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply