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 rate this snippet

How to iterate a dictionary in C# and VB.NET

To iterate a dictionary in C# and VB.NET you can use the following snippet.

Sample C#

var myDictionary = new Dictionary<int, string>();
myDictionary.Add(10, "Value1");
myDictionary.Add(20, "Value2");
myDictionary.Add(30, "Value3");

foreach (KeyValuePair<int, string> myPair in myDictionary)
{
    Console.WriteLine("{0} - {1}", myPair.Key, myPair.Value);
}

Sample VB.NET

Dim myDictionary = New Dictionary(Of Integer, String)()
myDictionary.Add(10, "Value1")
myDictionary.Add(20, "Value2")
myDictionary.Add(30, "Value3")

For Each myPair As KeyValuePair(Of Integer, String) In myDictionary
	Console.WriteLine("{0} - {1}", myPair.Key, myPair.Value)
Next

for more informations see KeyValuePair

Please rate this snippet