I have the following method:
public static bool IsBetween<T>(this IComparable<T> value, T lowerBound, T upperBound)
where T : IComparable<T>
{
Contract.Requires<>(value != null);
Contract.Requires<>(lowerBound != null);
Contract.Requires<>(upperBound != null);
Contract.Requires<>(upperBound.CompareTo(lowerBound) >= 0);
return IsBetween(value, lowerBound, upperBound, InclusionOptions.None);
}
public static bool IsBetween<T>(this IComparable<T> value, T lowerBound, T upperBound,
InclusionOptions options) where T : IComparable<T>
{
Contract.Requires<>(value != null);
Contract.Requires<>(lowerBound != null);
Contract.Requires<>(upperBound != null);
Contract.Requires<>(upperBound.CompareTo(lowerBound) >= 0); //Code Contracts Issue
...
}
The problem here is that it does not like my last requirement. It states CodeContracts: requires unproven: upperBound.CompareTo(lowerBound) >= 0
. I'm not really sure the proper way to fix this here. I need to make sure that when I do my comparison values that I actually have a real lowerBound and upperBound value and that the lowerBound value is not above the upperBound value.
Oh, and I can't use the actual < or > operators because you can't apply them to the type 'T'.
Lastly, I know this can be a separate question, but it's highly related... if someone knows why I still get CA1062 Code Analysis errors when I'm using Code Contracts v1.4.50126.1 please tell me what to do to fix it: CA1062: Microsoft.Design : In externally visible method 'MyClass.IsBetween<T>(this IComparable<T>, T, T), validate parameter 'upperBound' before using it.