There are no generic operators supportion in C#. Moreover you can't declare generic type restrictions which means that "this generic type has some operators". There are to ways to solve this.
First is to use Expressions. You can read about it in Marc Gravells article.
Second is to use dynamic. For example, generic Add() method may looks like this without exceptions:
public static T Add<T>(T arg1, T arg2)
{
dynamic a1 = arg1;
dynamic a2 = arg2;
return a1 + a2;
}
In both cases you may have some unpleasant surprises during the runtime.
But as I understand you want to add two objects of different types. Foo< int> is not the same as Foo< string> and I think this even has no sense. What the result of this adding will be? Anyway you may declare your method like this:
public static T1 Add<T1, T2>(T1 arg1, T2 arg2)
{
dynamic a1 = arg1;
dynamic a2 = arg2;
return a1 + a2;
}
This method can succesfully add int to string(but not vice versa):
Console.WriteLine(Add("hello", 1));
You may try to add anything to anything but you must have appropriate version of operator overloading to have no exceptions. Another example:
class MyClass<T>
{
public static MyClass<T> operator +(MyClass<T> c1, MyClass<int> c2)
{
return new MyClass<T>();
}
}
//...
// Add MyClass<string> to MyClass<int>.
// As you can see we have appropriate version of operator overloading for doing this without exceptions.
var c1 = new MyClass<string>();
var c2 = new MyClass<int>();
var res = Add(c1, c2);