46

I have this code:

List<int> myList = new List<int>();
var max = myList.Max();
Console.Write(max);

I want that to ensure that if there are no elements in the list it should use the default value for int (0). But instead an InvalidOperationException is being thrown, stating that the "Sequence contains no elements".

Of course I could use Any or the query syntax (as in here). But I want to do it using the fluent syntax.

How can I fix this?

5 Answers 5

108

Try this:

var myList = new List<int>();
var max = myList.DefaultIfEmpty().Max();
Console.Write(max);

LINQ's DefaultIfEmpty-method checks if the sequence is empty. If that is the case, it will return a singleton sequence: A sequence containing exactly one element. This one element has the default value of the sequence's type. If the sequence does contain elements, the DefaultIfEmpty-method will simply return the sequence itself.

See the MSDN for further information

1
  • 6
    Note that DefaultIfEmpty also has an overload that takes a user-provided default value (not what the OP asked for, but what I wanted)
    – dlf
    Commented Dec 3, 2018 at 21:59
9

What about an extension?

public static int MaxOrDefault<T>(this IEnumerable<T> enumeration, Func<T, int> selector)
{
    return enumeration.Any() ? enumeration.Max(selector) : default(int);
}
1
  • This combined with @BillStanton's answer is the best for me - avoids double (partial) iteration of the sequence. Commented May 13, 2019 at 16:36
4

Slightly at a tangent but perhaps still useful..another solution is to cast the value to nullable type;

var myInt = new Int[0].Max(i=>(int?)i);//myInt == null
4

If you use Enumerable.Aggregate, you won't need any special handling for the empty list case. Try this:

List<int> myList = new List<int>();
var max = myList.Aggregate(default(int), (seed, i) => (i > seed ? i : seed));
Console.Write(max);
0

As an extension method, the solution of @AlvaroRivoir can be extendend with further generalization adding another type parameter:

public static K MaxOrDefault<T, K>(this IEnumerable<T> enumeration, Func<T, K> selector) {
    return enumeration.Any() ? enumeration.Max(selector) : default;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.