Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type of the conditional expression can not be determined? [duplicate]

I just encountered this (made up code to demonstrate the "problem"):

public ICollection<string> CreateCollection(int x)
{
    ICollection<string> collection = x == 0 
                                   ? new List<string>() 
                                   : new LinkedList<string>();
    return collection;
}

The compiler complains:

Fehler CS0173: Der Typ des bedingten Ausdrucks kann nicht bestimmt werden, weil keine implizite Konvertierung zwischen "System.Collections.Generic.List" und "System.Collections.Generic.LinkedList" erfolgt.

Which translates roughly to:

The type of the conditional operator can not be determined, because there is no implicit conversion between List and LinkedList.

I can see why the compiler complains, but hey, come on. It is trying to play stupid. I can see that both expressions are not of the same type but have one common ancestor and as a bonus the type of the left side is also a common ancestor. I am sure the compiler can see it too. I could understand the error if the left side was declared as var.

What am I missing here?

Edit:

I am accepting James Gaunt's explanation. Maybe Just to make it clear. I can read the compiler spec just fine. I wanted to understand why. Why did someone make the decision to write the spec that way. There must be a reason behind that design. According to James the design principle is 'no surprises'. Also CodeInChaos explains what surprises you might encounter if the compiler would try to deduce the type from common ancestors.

like image 673
EricSchaefer Avatar asked Dec 12 '10 21:12

EricSchaefer


2 Answers

The expression (a ? b : c) has to resolve to a type. The type will be either the type of b or c. If these are different (and there isn't an implicit conversion from one to the other) the compiler doesn't know which type this is at compile time.

You may say it should deduce that there is common root type, but there is always a common root type (e.g. Object).

In general the C# compiler will not attempt to guess what you mean. If you want to use the common root type then cast b and c to that type.

This kind of logic runs throughout the design of C#, it is occasionally a bit annoying, but far more often it stops you making mistakes.

like image 149
James Gaunt Avatar answered Oct 12 '22 20:10

James Gaunt


Because of interfaces they can have multiple different common ancestors.

One could add a requirement that it only auto-converts if the ancestor is unambiguous. But then adding additional interfaces a class implements suddenly becomes a breaking change. And that might not be desirable.

For example suppose you make these types implement ISerializeable. This shouldn't change the behavior of your code, but if you supported that casting to common interface it would.

edit: Thought a bit more about it and noticed that this function already has exactly the same problem:

T MyFunc<T>(T left,T right)

And this code doesn't compile:

ICollection<string> r=MyFunc(new List<string>() , new LinkedList<string>());

because it can't decide which type to use as the type-parameter T. So the behavior of the ?: operator is consistent with overload resolution.

like image 25
CodesInChaos Avatar answered Oct 12 '22 19:10

CodesInChaos