Advertisement
NelIfandieva

Dictionaries_DisplayCount_DisplayKeysOfDict

Feb 24th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. Dictionary<string, List<double>> studentRecords = new Dictionary<string, List<double>>();
  2.             studentRecords["Angel"] = new List<double>();
  3.             studentRecords["Angel"].Add(5.50);
  4.             studentRecords["Angel"].Add(4.75);
  5.             studentRecords["Angel"].Add(5.25);
  6.             studentRecords["Bogomil"] = new List<double>();
  7.             studentRecords["Bogomil"].Add(5.57);
  8.  
  9.             Console.WriteLine(studentRecords.Count()); //Displays the number of KeyValuePairs ——> here now 2
  10.             //Down: Way 1 to display only the Keys in a Dict:
  11.             foreach(var keyValuePair in studentRecords)
  12.             {
  13.                 Console.WriteLine(keyValuePair.Key);//Displays only the Keys in a Dictionary (Way 1)
  14.             }
  15.  
  16.             //Down: Way 2 to display only the Keys in a Dict:
  17.             var keys = studentRecords.Keys;//Collection of the existing Keys in a Dictionary (Way 2)
  18.             foreach(var key in keys)
  19.             {
  20.                 Console.WriteLine(key); // Displays only the keys
  21.             }
  22.  
  23.             //Down: Way 3 to Display the Keys in a Dict
  24.             var listOfKeys = new List<string>();
  25.             foreach(var kVP in studentRecords)
  26.             {
  27.                 listOfKeys.Add(kVP.Key); //Adds only the Keys to a List
  28.             }
  29.             Console.Write(string.Join(", ", listOfKeys));//Displays the Keys
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement