display the Min, Max, Sum and Average - CSharp LINQ

CSharp examples for LINQ:IEnumerable

Description

display the Min, Max, Sum and Average

Demo Code

using System;// w w w .ja  va2  s  . c o m
using System.Collections.Generic;
using System.Linq;
class FunctionalProgramming
{
   static void Main()
   {
      var values = new List<int> {3, 10, 6, 1, 4, 8, 2, 5, 9, 7};
      Console.Write("Original values: ");
      values.Display(); // call Display extension method
      // display the Min, Max, Sum and Average
      Console.WriteLine($"\nMin: {values.Min()}");
      Console.WriteLine($"Max: {values.Max()}");
      Console.WriteLine($"Sum: {values.Sum()}");
      Console.WriteLine($"Average: {values.Average()}");
   }
}
// declares an extension method
static class Extensions
{
   // extension method that displays all elements separated by spaces
   public static void Display<T>(this IEnumerable<T> data)
   {
      Console.WriteLine(string.Join(" ", data));
   }
}

Result


Related Tutorials