Quick Tip: Iterate through an enum in .NET

Here’s a quicky for you. Iterating through an Enum in .NET, replace ‘IconResource‘ with the Enum you want to iterate.

CSharp


Array enumValues = System.Enum.GetValues(typeof(IconResource));

foreach (IconResource resource in enumValues)
{
	Console.WriteLine("Resource: {0}", resource);
}

VB.NET


Dim enumValues As Array = System.[Enum].GetValues(GetType(IconResource))

For Each resource As IconResource In enumValues
    Console.WriteLine("Resource: {0}", resource)
Next

Useful?

Related Articles

Comments have been disabled.