find the range of all keys in a map in csharp

To find the range of all keys in a C# dictionary, you can use the System.Linq namespace and the Min and Max extension methods. Here's an example:

main.cs
using System.Collections.Generic;
using System.Linq;

// create a dictionary
var dictionary = new Dictionary<int, string>();

// add some key-value pairs
dictionary.Add(1, "one");
dictionary.Add(2, "two");
dictionary.Add(3, "three");
dictionary.Add(4, "four");

// find the range of keys
int minKey = dictionary.Keys.Min();
int maxKey = dictionary.Keys.Max();

// print the range
Console.WriteLine($"Minimum key: {minKey}");
Console.WriteLine($"Maximum key: {maxKey}");
470 chars
20 lines

Output:

main.cs
Minimum key: 1
Maximum key: 4
30 chars
3 lines

In the example above, we created a dictionary and added some key-value pairs. Then, we used the Min and Max extension methods on the Keys property of the dictionary to find the minimum and maximum keys, respectively. Finally, we printed the results.

gistlibby LogSnag