To find the smallest key in a Map (Dictionary) in C#, you can use LINQ to sort the keys and then select the first one:
main.cs334 chars14 lines
In this example, the Dictionary contains three items with keys 3, 1, and 2. The Keys
property is used to get a collection of all the keys in the Dictionary. OrderBy()
is used to sort them in ascending order. And First()
is used to get the first (i.e. smallest) key in the sorted collection. Finally, the result is printed to the console.
gistlibby LogSnag