c# - Nullable generic type used with IComparable. Is it possible? -


I am trying to create a simple clamp (so that I can force the values ​​of some ... most numbers Type for example, int, double, etc.).

The problem is that if I do the following, I get an error, but compare the icpimperables should be able to handle zero values. Definition, an object compares more than zero, and two tap references are equal to one another.

(Max)> Maximum return; if (value.CompareTo (min) <0) return minutes; amount of return;} private int32? _zip; public int32? Zip {receive {return_zip;} set {_zip = value.clamp

Int32? a Nullable & lt; Int32 & gt; . Since Nullable does not apply IComerBarL Your code will not be compactly compiled.

You can, however, method overload:

  public static t? Clamp &  (this T? Value, T? Min, T? Max) where T: struct, iComparable  {// your argument ...}  

Of course, if you are planning to work with flexible types, then you have to determine how you null value ...

if you actually Do not suppress the null values, so just up Nine property recipients may be simple to check for zero:

  Public Int32? Zip {... set {_zip = value == empty? Value: value.Value.Clamp & lt; Int32 & gt; (0, 99 99); }  

Or better yet, make it part of the implementation of the clamp ...

of the additional surcharges .

Comments