InterviewSolution
Saved Bookmarks
| 1. |
What is a Dictionary collection in C#? |
|
Answer» DIFFERENT Key-Value pairs are represented using the dictionary collection. This is somewhat SIMILAR to an English dictionary which has different words along with their meanings. The dictionary collection is included in the System.Collections.Generic namespace. A program that demonstrates the dictionary collection in C# is given as follows: using System; using System.Collections.Generic; namespace Demo { CLASS Example { static VOID Main(string[] args) { Dictionary<int, string> dict = new Dictionary<int, string>(); dict.Add(1,"Apple"); dict.Add(2,"Mango"); dict.Add(3,"Orange"); dict.Add(4,"Guava"); dict.Add(5,"Kiwi"); Console.WriteLine("The dictionary elements are given as follows:"); foreach (KeyValuePair<int, string> i in dict) { Console.WriteLine("Key: {0} Value: {1}", i.Key, i.Value); } } } }The output of the above program is as follows: The dictionary elements are given as follows: Key: 1 Value: Apple Key: 2 Value: Mango Key: 3 Value: Orange Key: 4 Value: Guava Key: 5 Value: Kiwi |
|