Basically i have a specific method which uses double and int values to figure out a css class i want to set. I'm doing the same in a different place but with int + int there instead.
That spawned the question whether there is a good way to have a one for all solution.
Overloads would have been one way to do it but i would love to not write an overload for all variations of numbers.
So i thought i'd have a look wether there is a number specific interface type i can use as generic type constraint - but i didn't find one (since anything could implement IConvertible?)
Result of int32 decompile :
#if GENERICS_WORK
public struct Int32 : IComparable, IFormattable, IConvertible
, IComparable<Int32>, IEquatable<Int32>
/// , IArithmetic<Int32>
#else
public struct Int32 : IComparable, IFormattable, IConvertible
#endif
Nope, no number interface.
This is what i came up with - which works fine, but also accepts calls on potential non number objects. Any suggestions on how to make the method more restrictive?
public static string GetThresholdColorClass(IConvertible desiredThreshold, IConvertible actualProgress)
{
var actual = actualProgress.ToDouble(CultureInfo.InvariantCulture);
var desired = desiredThreshold.ToDouble(CultureInfo.InvariantCulture);
if (actual >= desired)
return "green";
if (actual <= 0)
return "red";
return "yellow";
}