WebApr 6, 2024 · This article and code example teach you how to use enums in C# and .NET. A C# enum data type represents multiple values. Each item of an enum is represented by … WebMay 12, 2016 · public static class EnumUtil { public static IEnumerable GetValues () { foreach (T enumVal in Enum.GetValues (typeof (T))) { yield return enumVal; } } } Which is then used like so: var s = string.Join (", ", EnumUtil.GetValues () .Where (x => (myIntValue & (int)x) != 0)); Share Improve this answer Follow
forEach and Enumerables in C# Pluralsight
WebEnumerate an enum in C# This post will discuss how to loop through all values of enum in C#. 1. Using Enum.GetNames()method The idea is to use the Enum.GetNames()method to get an array of the names of the constants in the enum and print the array using the foreach loop. 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 … WebApr 14, 2024 · string[] fruits = input.Split(delimiterChars, 3); foreach (string fruit in fruits) {. Console.WriteLine(fruit); } } } We use the Split method to split a string into an array of substrings based on an array of delimiter characters. We limit the number of substrings returned to 3 and output each element to the console. imperial clone shock trooper
How to loop through all enum values in C# - C# Corner
WebSep 10, 2015 · Enum.GetValues returns values for the enum as objects, so you need to cast them to int value (using int instead of var will do that for you). After that you can use Enum.GetName to get the name for specific value: WebThere are two ways to iterate an Enum: 1. var values = Enum.GetValues (typeof (myenum)) 2. var values = Enum.GetNames (typeof (myenum)) The first will give you values in form … WebFeb 28, 2024 · The Enum.The GetValues method returns an array of all enum values. The following code snippet loops through all values of an enum and prints them on the … imperial clone shock troopers