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).