Summary: in this tutorial, you’ll learn how to use the LINQ Max()
method to get the maximum value in a sequence.
Introduction to the LINQ Max() method
The LINQ
is an extension method that returns the maximum number in a sequence of numbers. Here’s the syntax of the Max()
method:Max()
public static TResult? Max (
this IEnumerable<TSource?> source
);
Code language: C# (cs)
In this syntax:
TSource
is the type of element in thesource
sequence.source
is the input sequence with the IEnumerable<T>.
The Max()
returns the maximum value of the source
sequence. The method ignores null
in the source
sequence.
If the source
sequence is empty, the Max()
method throws an InvalidOperationExceptoin
. In case the source
sequence is null
, the method throws an ArgumentNullexception
.
The Max()
method has an overload that accepts a selector function as the second argument:
public static TResult? Max (
this IEnumerable<T?> source
Func<TSource,TResult> selector
);
Code language: C# (cs)
The selector
is a transform function that applies to each element of the source
sequence.
LINQ Max() method examples
Let’s take some examples of using the LINQ Max()
method.
1) Using the LINQ Max() method to find the maximum value in an array of integers
The following program demonstrates how to use the Max()
method to find the maximum number in an array of integers:
int[] numbers = { 5, 2, 8, 3, 7 };
int max = numbers.Max();
Console.WriteLine($"Maximum value = {max} ");
Code language: C# (cs)
Output:
Maximum value = 8
Code language: C# (cs)
2) Using the LINQ Max() method with a selector function example
The following example demonstrates how to use the Max()
method with a selector function to find the maximum length of a sequence of strings:
string[] words = { "apple", "banana", "mango" };
int maxLength = words.Max(word => word.Length);
Console.WriteLine($"Maximum length = {maxLength}");
Code language: C# (cs)
Output:
Maximum length = 6
Code language: C# (cs)
3) Using the LINQ Max() method to find a string with an empty sequence
If the input sequence is empty, the Max()
method throws an InvalidOperationException
. In this case, you can use the DefaultIfEmpty()
method to specify a default maximum value and avoid the exception. For example:
int[] lengths = { };
var maxLength = lengths.DefaultIfEmpty(0).Max();
Console.WriteLine($"Maximum value = {maxLength}");
Code language: C# (cs)
Output:
Maximum value = 0
Code language: C# (cs)
4) Using the LINQ Max() method to find an object with a maximum key’s value in a sequence
Suppose you have a Product
class that has three properties Name
, Cost
, and Price
:
public class Product
{
public string Name
{
get; set;
}
public decimal Price
{
get; set;
}
public decimal Cost
{
get; set;
}
public Product(string name, decimal cost, decimal price)
{
Name = name;
Cost = cost;
Price = price;
}
public override string ToString() => $"{Name}, Cost:{Cost}, Price:{Price}";
}
Code language: JavaScript (javascript)
The following program uses the Max()
method to find the highest price among all the products in a list:
using static System.Console;
var products = new List<Product>() {
new Product("A",100,160),
new Product("B",95,130),
new Product("C",140,150),
};
var product = products.Max(p => p.Price);
WriteLine(product);
Code language: JavaScript (javascript)
Output:
160
The following selector function instructs the Max()
method to compare products by the Price
property and return the highest price:
p => p.Price
Code language: PHP (php)
If you want to find the product with the highest cost, you can change the selector function to the following:
p => p.cost
Code language: PHP (php)
For example:
using static System.Console;
var products = new List<Product>() {
new Product("A",100,160),
new Product("B",95,130),
new Product("C",140,150),
};
var product = products.Max(p => p.Cost);
WriteLine(product);
Code language: JavaScript (javascript)
Output:
140
Summary
- Use the LINQ
Max()
method to find the maximum value in a sequence. - Use the
DefaultIfEmpty()
method with theMax()
method to specify a default maximum value if the sequence is empty.