Home
Map
Max and Min MethodsUse Max and Min from System.Linq. Get the highest or lowest elements.
C#
This page was last reviewed on Apr 25, 2023.
Max, Min. In C# programs we call Max (and Min) from System.Linq to get the largest or smallest element. Each element is iterated in the search.
Shows an array
These methods can also be used with lambda expressions. Higher-order functions can make programs shorter (but sometimes slower and harder to read).
Lambda
Max. To begin, we must include System.Linq for this program to compile. Max() is an extension method (found in the Linq namespace).
Step 1 This program instantiates an array of 4 ints. There are 2 negative numbers in the array.
Step 2 Next the Max method is called upon this array. Max returns 1 because 1 is the largest value in the program's array.
Step 3 We use Max with a lambda expression. The value 2 is returned—the absolute value of -2 is 2, and 2 is the maximum.
int Array
Math.Abs
using System; using System.Linq; // Step 1: create array. int[] array1 = { 1, -1, -2, 0 }; // Step 2: use simple Max to find maximum number. Console.WriteLine(array1.Max()); // Step 3: find maximum number when all numbers are made positive. Console.WriteLine(array1.Max(element => Math.Abs(element)));
1 2
Min. We want to find the minimum value in a collection (such as an array or List) in a simple way. With Min() we find the minimum element or the minimum value after a transformation.
Array
List
Part 1 The first call to the Min() method determines that -1 is the smallest integer.
Part 2 The second call changes the 2 to -2, so that is now the smallest integer.
Info The values in the source array (array1) are not mutated. Min() provides a clear way to find the smallest value in a collection.
Also You can provide a transformation function, which provides a mechanism for you to insert additional logic if needed.
Shows an array
using System; using System.Linq; int[] array1 = { 1, -1, 2, 0 }; // Part 1: find minimum number. Console.WriteLine(array1.Min()); // Part 2: find minimum number when all numbers are made negative. Console.WriteLine(array1.Min(element => -element));
-1 -2
Math.Max. The Math class contains some math methods like Max and Min that also find the lower or higher number. But these are simpler and only test 2 numbers.
Math.Max, Math.Min
A summary. The Max function can find either the maximum value in a collection, or the maximum value after a specific transformation is applied. Min returns the minimum value.
Notes. These methods are perhaps most useful with a lambda expression: this makes the computation adjustable. Other forms of delegates (not just lambdas) can be used.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Apr 25, 2023 (rewrite).
Home
Changes
© 2007-2024 Sam Allen.