Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does IEnumerable .Min handle Nullable types?

Tags:

c#

So, IEnumerable uses the IComparable interface to evaluation a call to .Min(). I'm having trouble finding whether or not the nullable types support this. Assuming I have a list of int?, {null, 1, 2}. Will .Min() work?

like image 970
Russell Steen Avatar asked Mar 23 '11 22:03

Russell Steen


1 Answers

The following program

using System;
using System.Collections.Generic;
using System.Linq;

public class Test
{
    public static void Main()
    {
        List<int?> l = new List<int?>() {1, null, 2};
        Console.WriteLine(l.Min());
    }
}

outputs 1. If the list is however empty, or contains only null, the output is null.

So null counts as the biggest int for Min.

like image 127
Vlad Avatar answered Oct 18 '22 19:10

Vlad