I'm trying to create a simple Clamp (so that I can bound the values of anything comparable ... mostly for number types such as int, double, etc.)
The problem is if I do the following I get an error, but according to MSDN IComparable's CompareTo is supposed to be able to handle null values.
Quote: "By definition, any object compares greater than null, and two null references compare equal to each other."
public static T Clamp<T>(this T value, T min, T max)
where T : IComparable<T>
{
if (value.CompareTo(max) > 0)
return max;
if (value.CompareTo(min) < 0)
return min;
return value;
}
private Int32? _zip;
public Int32? Zip
{
get
{
return _zip;
}
set
{
_zip = value.Clamp<Int32?>(0, 99999);
}
}
Remember, Int32?
is a shorthand for Nullable<Int32>
. Since Nullable<T>
does not implement IComparable<T>
, your code, as structured, won't compile.
You can, however, overload the method:
public static T? Clamp<T>(this T? value, T? min, T? max)
where T : struct, IComparable<T>
{
// your logic...
}
Of course, if you're planning on working with nullable types, you have to define how you will clamp null
values...
If you don't actually need to clamp null
values, it may be simpler to just first check for null in your property getter:
public Int32? Zip
{
...
set
{
_zip = value == null ? value : value.Value.Clamp<Int32>(0,99999);
}
Or better yet, make it part of the implementation of the additional overload to Clamp
...