Dilemma with two types and operator +

Posted by user35443 on Stack Overflow See other posts from Stack Overflow or by user35443
Published on 2012-06-17T09:12:08Z Indexed on 2012/06/17 9:15 UTC
Read the original article Hit count: 156

Filed under:
|
|
|
|

I have small problem with operators. I have this code:

public class A
{
    public string Name { get; set; }
    public A()
    { 

    }
    public A(string Name)
    {
        this.Name = Name;
    }
    public static implicit operator B(A a)
    {
        return new B(a.Name);
    }
    public static A operator+(A a, A b)
    {
        return new A(a.Name + " " + b.Name);
    }
}
public class B
{
    public string Name { get; set; }
    public B()
    { 

    }
    public B(string Name)
    {
        this.Name = Name;
    }
    public static implicit operator A(B b)
    {
        return new A(b.Name);
    }
    public static B operator +(B b, B a)
    {
        return new B(b.Name + " " + a.Name);
    }
}

Now I want to know, which's conversion operator will be called and which's addition operator will be called in this operation:

new A("a") + new B("b");

Will it be operator of A, or of B? (Or both?) Thanks....

© Stack Overflow or respective owner

Related posts about c#

Related posts about oop