I got a strange behaviour and I would like to understand it. But I haven't found good answers on the web :(
Here's the situation, I've abstracted the name and logic to focus on the issue. Got 3 types, A, B and C. B & C have implicit operators defined to convert to A object.
public class A
{
public static implicit operator A(B input){ /* Convert B to A */ }
public static implicit operator A(C input) { /* Convert C to A*/ }
}
public class B { }
public class C { }
Then, when I do this, the code compile and work fine :
A myObject = null;
if (condition)
myObject = new B();
else
myObject = new C();
But when I write the same logic with an inline if, I got an error :
A myObject = condition ? new B() : new C();
The error :
Type of conditional expression cannot be determined because there is no implicit conversion between 'B' and 'C'
Do you have any idea about this behaviour ?
Thank's in advance for your time.
Best regards and keep it bug free !