C# dictionary get value by key

C# dictionary get value by key

Dictionary<TKey, TValue>

Create the Dictionary<TKey, TValue> object by passing the type of keys and values it can store.

Dictionary<TKey, TValue> is a generic collection that stores key-value pairs in no particular order.

 

using System;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
		IDictionary<int, string> nuNames = new Dictionary<int, string>();
		nuNames.Add(1,"One");		
		nuNames.Add(2,"Two");
		nuNames.Add(3,"Three");
		nuNames.Add(4,"Four");
		nuNames.Add(5,"Five");
		nuNames.Add(6,"Six");
		
		foreach(KeyValuePair<int, string> nu in nuNames)
			Console.WriteLine("Key: {0}, Value: {1}", nu.Key, nu.Value);
		
		
		
	}
}

//Output
//Key: 1, Value: One
//Key: 2, Value: Two
//Key: 3, Value: Three
//Key: 4, Value: Four
//Key: 5, Value: Five
//Key: 6, Value: Six

 

 

2021-10-03