To find the largest key in a Dictionary
(which is a type of map) in C#, you can use LINQ to sort the keys and select the last one:
main.cs240 chars8 lines
Here, OrderByDescending
sorts the keys from largest to smallest, and First
returns the first (i.e. largest) one in the sequence. If the dictionary is empty, attempting to access First
will throw an exception, so you may want to add a check for that before calling this code.
gistlibby LogSnag