In C++, you can write code like this:
template<class T>
T Add(T lhs, T rhs)
{
return lhs + rhs;
}
But, you can't do something like this in C#:
public static T Add<T>(T x, T y) where T : operator+
{
return x + y;
}
Is there any reason why? I know it could be accomplished through reflection (generic Add
with objects and then run type checking over it all), but that's inefficient and doesn't scale well. So, again, why?