EN
C# / .NET - find max and min value in a List
0 points
In this article, we would like to show you how to find max and min value in a List in C#.
Quick solution:
xxxxxxxxxx
1
List<int> myList = new List<int> { 2, 1, 4, 3 };
2
int min = myList.Min();
3
int max = myList.Max();
4
5
Console.WriteLine(min); // 1
6
Console.WriteLine(max); // 4
In this example, we use Min()
and Max()
methods from System.Linq
to get the minimum and maximum value from List of integers.
xxxxxxxxxx
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
public class Program
5
{
6
public static void Main()
7
{
8
List<int> myList = new List<int> { 2, 1, 4, 3 };
9
int min = myList.Min();
10
int max = myList.Max();
11
12
Console.WriteLine("Min = " + min);
13
Console.WriteLine("Max = " + max);
14
}
15
}
Output:
xxxxxxxxxx
1
Min = 1
2
Max = 4