c# - Creating a nullable<T> extension method ,how do you do it? -
i have situation need compare nullable types.
suppose have 2 values:
int? foo=null; int? bar=4; this not work:
if(foo>bar) the following works not nullable restrict value types:
public static bool islessthan<t>(this t leftvalue, t rightvalue) t : struct, icomparable<t> { return leftvalue.compareto(rightvalue) == -1; } this works it's not generic:
public static bool islessthan(this int? leftvalue, int? rightvalue) { return nullable.compare(leftvalue, rightvalue) == -1; } how make generic version of islessthan?
thanks lot
try this:
public static bool islessthan<t>(this nullable<t> t, nullable<t> other) t : struct { return nullable.compare(t, other) < 0; }
Comments
Post a Comment