0

Consider this simple code in C++/CLI

template <typename T>
T sum (T x, T y)
{
    return x + y;
}

int main(array<System::String ^> ^args)
{
    int a=4, b=6;
    double x=2.3, y=5.2;

    Console::WriteLine("Sum of two ints = {0}", sum(a, b));
    Console::WriteLine("Sum of two doubles = {0}", sum(x, y));

    return 0;
}  

Output:

Sum of two ints = 10
Sum of two doubles = 7.5

How can I do this in C# using generics?

Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • 1
    possible duplicate of [Arithmetic operator overloading for a generic class in C#](http://stackoverflow.com/questions/756954/arithmetic-operator-overloading-for-a-generic-class-in-c-sharp) – Håkan Fahlstedt Feb 13 '14 at 18:00

2 Answers2

0

You can't. There is no way to apply a generic constraint in C# that will ensure that the given generic type will have a suitable overload of the + operator.

You can remove all static type checking and use reflection or similar to attempt to call the + operator of the given generic type at runtime, realizing that it will simply throw a runtime exception if no applicable operator exists, but that is not equivalent to the C++ code you provided.

Servy
  • 202,030
  • 26
  • 332
  • 449
0

There's no simple way of doing it with generics, because generics are not templates. You can do it if you supply the operator yourself, like this:

static T binary_apply<T>(T a, T b, Func<T,T,T> add) {
    return add(a, b);
}

int a = 4, b = 6;
double x = 2.3, y = 5.2;
Console.WriteLine("Sum of two ints = {0}", binary_apply(a, b, (i,j)=>i+j));
Console.WriteLine("Sum of two doubles = {0}", binary_apply(x, y, (i,j)=>i+j));

Of course this defeats the purpose of having add<T>() in the first place, because you pass an implementation of the addition as a lambda. You can do this dynamically, too, but that is not the same as having the addition resolved by the compiler:

static T add<T>(T a, T b) {
    var p0 = Expression.Parameter(typeof(T));
    var p1 = Expression.Parameter(typeof(T));
    var ae = Expression.Add(p0, p1);
    var f = (Func<T,T,T>)Expression.Lambda(ae, p0, p1).Compile();
    return f(a, b);
}

int a = 4, b = 6;
double x = 2.3, y = 5.2;
Console.WriteLine("Sum of two ints = {0}", add(a, b));
Console.WriteLine("Sum of two doubles = {0}", add(x, y));

Demo on ideone.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you for the demo. However, two questions 1) The method add must be static? 2) What is Expression. Interface? Abstract class? Where can I study this stuff? Thank you. – user3307184 Feb 13 '14 at 18:23
  • @user3307184 (1) `static` functions of C# are the closest thing to "free-standing" functions of C++. You can make the function non-static if you have to, but in this case it would unnecessarily complicate things. (2) `Expression` is a LINQ class that lets you build expressions at runtime. [Here is a link to its documentation](http://msdn.microsoft.com/en-us/library/system.linq.expressions.expression(v=vs.110).aspx). – Sergey Kalinichenko Feb 13 '14 at 18:34