的有条件前pression类型无法确定? [英] The type of the conditional expression can not be determined?

查看:186
本文介绍了的有条件前pression类型无法确定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是碰到了这个(由code表现出了问题):

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:明镜典型德bedingten Ausdrucks卡恩nicht bestimmt werden,韦尔keine implizite Konvertierung zwischenSystem.Collections.Generic.ListUNDSystem.Collections.Generic.LinkedListerfolgt

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.

大致解释:

条件运算符的类型无法确定,因为没有目录和LinkedList之间的隐式转换。

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

我可以看到为什么编译器抱怨,但是,嘿,来吧。它试图打懵。我可以看到,无论前pressions是同一类型的不但有一个共同的祖先,并作为奖金的左侧的类型也是一个共同的祖先。我相信编译器可以看到这一点。我能理解的错误,如果左侧被宣布为 VAR

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.

我是什么在这里失踪?

编辑:

我在接受詹姆斯·冈特的解释。也许只是为了说清楚。我可以读编译器规格就好了。我想知道为什么。为什么有人做写规范这样的决定。有必须是设计背后的理由。据詹姆斯的设计原则是没有惊喜。另外codeInChaos解释什么惊喜,你可能会遇到如果编译器将设法推断从共同的祖先类型。

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.

推荐答案

这位前pression(A,B:C)的解析为一个类型。的类型将是B或C的任一类型。如果这些是不同的(并没有隐式转换,从一个到另一个)编译器不知道哪种类型,这是在编译时。

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

在一般的C#编译器不会试图去猜测你的意思。如果你想使用普通根型再抹上b和c那个类型。

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.

这样的逻辑贯穿C#的设计,这是偶尔有点讨厌,但是的的更多的时候它阻止你犯错。

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.

这篇关于的有条件前pression类型无法确定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆