Can anyone tell me if there is a way with generics to limit a generic type argument T
to only:
Int16
Int32
I created a little library functionality to solve these problems:
Instead of:
public T DifficultCalculation(T a, T b)
{
T result = a * b + a; // <== WILL NOT COMPILE!
return result;
}
Console.WriteLine(DifficultCalculation(2, 3)); // Should result in 8.
You could write:
public T DifficultCalculation(Number a, Number b)
{
Number result = a * b + a;
return (T)result;
}
Console.WriteLine(DifficultCalculation(2, 3)); // Results in 8.
You can find the source code here: https://codereview.stackexchange.com/questions/26022/improvement-requested-for-generic-calculator-and-generic-number