find the range of a list of numbers in csharp

To find the range of a list of numbers in C#, you can use the System.Linq namespace and the Max and Min methods of IEnumerable<T>. Here's an example:

main.cs
using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        int[] numbers = { 10, 5, 7, 3, 8 };
        int range = numbers.Max() - numbers.Min();
        Console.WriteLine($"The range of the list is {range}.");
    }
}
260 chars
13 lines

Output:

main.cs
The range of the list is 7.
28 chars
2 lines

In this example, we first create an array of integers numbers containing the list of numbers. We then use the Max and Min methods to find the maximum and minimum values in the array, respectively. The range is then calculated as the difference between the maximum and minimum values. Finally, we print the result to the console.

gistlibby LogSnag