posted 7/25/2010 2:43:42 PM 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.
Raghav Khunger (Member since: 4/19/2009 1:46:52 AM)
View Raghav Khunger 's profile
Leave a comment
It's fast, easy and free! Submit articles, get your own blog, ask questions & give answers in the forums, and become a better developer, faster.
enter your email address: