Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#. Error CS8135: Tuple with 2 elements cannot be converted to type 'object'

Tags:

c#

valuetuple

Consider the following method:

public object Foo(bool flag)
{
    if (flag) 
        return (new object(), new object());
    return (null, new object()); //Compiler error over here!!
}

This does not compile showing the error I mentioned in the title of this question. I can fix that with just a cast like the following:

public object Foo(bool flag)
{
    if (flag) 
        return (new object(), new object());
    return ((object)null, new object());
}

So far so good. The weird part and the reason for which I am asking this is that if I change the syntax and I use ternary operator instead of if-else statement like this:

public object Boo(bool flag) => flag
                                ? (new object(), new object())
                                : (null, new object());

Then no cast is needed!!! Why? IMHO both ways of writing the method are semantically equal. I know that the IL generated may not be the same (haven't checked that).

like image 448
taquion Avatar asked Feb 15 '26 03:02

taquion


1 Answers

The problem is that the null expression has no definite type, it depends on the context in which it is used. In the case of the conditional operator, the compiler has to find a common type between (object, object) and (null, object). In that case it can deduce that null may be object.

When the compiler sees (null, object) and needs to convert to object, the value null can be a multitude of types thus the error. It's the same reason why it cannot infer a type for lambdas unless you give it a hint.

Action x = () => { }; // ok
var y = () => { }; // error

As an aside, rather than using (object)null as the value, I would strongly suggest using default(object) instead that way you're not tying yourself to a specific value, but rather a value appropriate for the type you give.

like image 180
Jeff Mercado Avatar answered Feb 17 '26 15:02

Jeff Mercado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!