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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…