2

Is there a way to make this code compile?

class Foo<T>
{
    ...
}

...

var a = new Foo<int>();
var b = new Foo<string>();
var c = a + b;

It seems that C# allows to specify neither operator+(Foo<int> a, Foo<string> b), nor operator+<U>(Foo<T> a, Foo<U> b). Given that I can easily create operator +(Foo a, Bar b) for non-generic Foo and Bar, this restriction, if it exists, seems to be somewhat weird.

hr0nix
  • 21
  • 2
  • http://stackoverflow.com/a/756995/961113 – Habib Jan 30 '14 at 19:40
  • 1
    I don't think that question has a lot to do with mine. Am I missing something obvious? – hr0nix Jan 30 '14 at 19:42
  • This one should be related: http://stackoverflow.com/questions/5905563/c-sharp-generic-operators – Paolo Tedesco Jan 30 '14 at 19:59
  • These questions are vaguely related to mine, if at all. Perhaps I should make the question more clear. – hr0nix Jan 30 '14 at 20:19
  • And what type of the result will be? – Deffiss Jan 30 '14 at 20:28
  • If I understand your question correctly, Marc Gravell's generic operators [solution](http://www.yoda.arachsys.com/csharp/genericoperators.html) should fit the bill, and that was linked in the accepted answer for one of the questions already brought up as a possible duplicate. – Sven Grosen Jan 30 '14 at 20:30

1 Answers1

0

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);
Deffiss
  • 1,136
  • 7
  • 12
  • Perhaps it should have been clarified in the question text: I don't intend to do any actual addition on generic values inside the plus operator, so the first part of the answer is not relevant. What I want to do is to implicitly build a tree corresponding to some arithmetic expression involving members of my custom type. Overloading with just one argument type fully specified seems to do the trick, thanks. – hr0nix Jan 30 '14 at 21:58