Creating a nullable extension method ,how do you do it?

后端 未结 2 1843
我寻月下人不归
我寻月下人不归 2021-02-19 00:54

I have a situation where I need to compare nullable types.
Suppose you have 2 values:

int? foo=null;
int? bar=4;

This will not work:

<
相关标签:
2条回答
  • 2021-02-19 01:32

    It can be simplified:

    public static bool IsLessThan<T>(this T? one, T? other) where T : struct
    {
        return Nullable.Compare(one, other) < 0;
    }
    
    0 讨论(0)
  • 2021-02-19 01:44

    Try this:

    public static bool IsLessThan<T>(this Nullable<T> t, Nullable<T> other) where T : struct
    {
        return Nullable.Compare(t, other) < 0;
    }
    
    0 讨论(0)
提交回复
热议问题