How to enumerate an Enum in C# and VB.NET

To enumerate an Enum in C# and VB.NET you can use the snippet below.

Sample C#

public enum TestEnum
{
	These = 0,
	Are = 1,
	Sample = 2,
	Enums = 3,
	Duplicates = 3,
	Included = 4
}

static void Main(string[] args)
{
	foreach (var testEnum in Enum.GetNames(typeof(TestEnum)))
	{
		System.Console.WriteLine(testEnum);
	}
	Console.Read();
}

Sample VB.NET

Public Enum TestEnum
	These = 0
	Are = 1
	Sample = 2
	Enums = 3
	Duplicates = 3
	Included = 4
End Enum

Private Shared Sub Main(args As String())
	For Each testEnum As var In [Enum].GetNames(GetType(TestEnum))
		System.Console.WriteLine(testEnum)
	Next
	Console.Read()
End Sub

Please Share us:
Please rate this snippet

4 thoughts on “How to enumerate an Enum in C# and VB.NET

Leave a Reply