获取 DateTime 类型的 Max 方法

get Max method for DateTime type

我正在寻找 returns 和 DateTimeEnumerable.Max 方法,但似乎不存在此方法。

我尝试使用反射来查找它,但是 none 结果返回 DateTime:

Dim test = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).
    Where(Function(m) m.Name = "Max")

有这样的方法吗?

Enumerable 没有为 DateTime 类型指定的 Max 方法,但它有通用的 Max(IEnumerable<TSource>) 方法可以处理 类 的对象,实现 IComparable<T>IComparable 接口,包括 DateTime.

所以你可以使用这个方法:

class Program
{
    delegate DateTime MaxDelegate(IEnumerable<DateTime> values);

    static void Main(string[] args)
    {
        MaxDelegate d = Enumerable.Max<DateTime>;
        var values = new DateTime[] { DateTime.MinValue, DateTime.Now, DateTime.MaxValue };
        var result = d(values);
        Console.WriteLine(result);
    }
}