Skip to main content

How to Find the Largest Value with LINQ Max in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

What's the highest score? How long is the longest word? In C#, LINQ's Max answers both: on its own it returns the largest value in a collection, and with a small function it returns the largest value it works out from each item.

Syntax​

Syntax
<Type> <largest> = <items>.Max();
<Type> <largest> = <items>.Max(<item> => <value from item>);
PartWhat it does
<items>.Max()The largest value in the collection, such as the highest number in an int[]
<items>.Max(<item> => <value from item>)Works out a value from each item, and returns the largest of those values

Microsoft's Enumerable.Max reference describes the first form for whole numbers: "Returns the maximum value in a sequence of Int32 values." And the second: "Invokes a transform function on each element of a sequence and returns the maximum Int32 value."

The Example​

The program finds the highest of three test scores, then the length of the longest word in a short list of fruit.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which prints the Max of the scores 72, 95 and 88, and the Max of the Length of each word in fig, banana and kiwi.

main.cs
int[] scores = [72, 95, 88];
Console.WriteLine(scores.Max());

string[] words = ["fig", "banana", "kiwi"];
Console.WriteLine(words.Max(word => word.Length));

word => word.Length is a lambda: for each word, it gives its Length.

Step 2: Run It​

Terminal
dotnet run main.cs
Output
95
6

A zsh terminal where dotnet run main.cs prints 95 and 6.

  1. 95 is the highest of the three scores.
  2. The lengths were 3, 6 and 4, so Max returned 6: the length, not the word.

The command is the same in zsh, bash and PowerShell.

An Empty Collection Throws​

With nothing to compare, Max has no answer:

main.cs
int[] scores = [];
Console.WriteLine(scores.Max());
Output
Unhandled exception. System.InvalidOperationException: Sequence contains no elements
at System.Linq.ThrowHelper.ThrowNoElementsException()
at System.Linq.Enumerable.MinMaxInteger[T,TMinMax](IEnumerable`1 source)
at Program.<Main>$(String[] args) in /Users/you/demo/main.cs:line 2

When a collection can be empty, check first and choose what "no scores" means:

main.cs
int[] scores = [];
Console.WriteLine(scores.Length > 0 ? scores.Max() : 0);
Output
0

The Item, Not the Value: MaxBy​

To get the longest word itself rather than its length, use MaxBy with the same lambda:

main.cs
string[] words = ["fig", "banana", "kiwi"];
Console.WriteLine(words.MaxBy(word => word.Length));
Output
banana

Common Mistakes​

MistakeWhat happensDo this instead
Calling Max() on a collection that can be emptyInvalidOperationException: Sequence contains no elementsCheck Length or Count first
Expecting Max(word => word.Length) to return the wordIt returns the largest length, 6Use MaxBy for the item

Troubleshooting​

SymptomLikely causeFix
Sequence contains no elementsMax ran on an empty collectionHandle the empty case before calling it
A number came back where you expected an itemMax with a lambda returns the valueSwitch to MaxBy

FAQ​

Does Max need a using directive?​

Not here: Max is a LINQ method from System.Linq, and a file run with dotnet run includes that namespace by default, so the example has no using line.

Is there a Min too?​

Yes. Min works the same way for the smallest value, and MinBy for the item: on the same data, scores.Min() gave 72, and words.MinBy(word => word.Length) gave fig.

Conclusion​

items.Max() returns the largest value, items.Max(item => value) the largest value worked out from each item, and items.MaxBy(item => value) the item that has it. On an empty collection, Max throws, so check first when it can be empty.

Additional Resources​