posted 7/25/2010 by Raghav Khunger
Yesterday a person asked on the forums on how to find the maximum key from a dictionary. He needs to perform some logic after finding the maximum key so I gave him the solution to use Max of LINQ. Below is the code for it :
#region Using Directives using System; using System.Collections.Generic; using System.Linq; #endregion public class Example { class Test { static void Main() { var dictionary = new Dictionary<int, string> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, }; var maxKey = dictionary.Max(x => x.Key); Console.WriteLine(maxKey); Console.ReadLine(); } } }
Above I have used a dummy dictionary, added few records in it. Then I have used the "Max" extension on "dictionary" object in order to extract the maximum key from it.
Do let me know your feedback, comments.
What kind of email newsletter would you prefer to receive from CodeAsp.Net?18