C# Dictionary

2075 / C# / Списки / Dictionary

 

Колекція ключів та значень (Key, Value)

Key - має бути унікальним

 

Dictionary<int, string> countries = new Dictionary<int, string>(4); 
countries.Add(1, "Ukraine"); 
countries.Add(3, "USA"); 
countries.Add(2, "Japan"); 

for (int i = 0; i < countries.Count; i++) { 
  var item = countries.ElementAt(i); 
  var itemKey = item.Key; 
  var itemValue = item.Value; 
}

foreach (KeyValuePair<int, string> item in countries) 
{ 
  Console.WriteLine(item.Key + " - " + item.Value); 
} 

1 – Ukraine
3 – USA
2 – Japan