为什么编译器不将 var[] 转换为 c# 中的 object[]?
这两行之间没有区别,因为编译器在第二行中理解它是一个 int 类型的数组。
var x = new int[] { 1, 2, 3 }; //Fine, x is int[]
var x = new [] { 1, 2, 3 }; //Fine, x is int[]
但为什么我不能对不同类型执行此操作?为什么编译器不将我的变量转换为 object 类型?
var x = new object[] { 1, "df", 5 }; //Fine, x is object[]
var x = new [] { 1, "df", 5 }; //Error! "No best type found for implicity-typed-array"
编辑:
感谢您的所有回答。但我仍然想知道,使编译器无法转换为 object
类型的所有表达式有何优缺点? (因为我使用 var
表示法,这意味着它不能是任何类型。我是这样理解的。)为什么编译器不通过继承树向上查找数组成员的最近类型?
There is no difference between these two lines, because the compiler, in the second line, understands that it is an array of type int.
var x = new int[] { 1, 2, 3 }; //Fine, x is int[]
var x = new [] { 1, 2, 3 }; //Fine, x is int[]
But why can't I do this with different types? Why doesn't the compiler convert my variable to type object?
var x = new object[] { 1, "df", 5 }; //Fine, x is object[]
var x = new [] { 1, "df", 5 }; //Error! "No best type found for implicity-typed-array"
EDIT:
Thanks for all your answers. But I still wonder, what are the pros and cons to make all expressions that the compiler can't convert to type object
? (Because I use var
notation which means that it can't be any type. I understand like this.) Why doesn't the compiler find the nearest type of the array members by going up the inheritance tree?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
new []
表示法用于节省您键入数组成员的显式类型(或允许您创建其元素具有匿名类型的数组),但其类型推断受到限制,因为所有元素必须共享相同的类型,或者可以隐式转换为至少一个成员共享的公共类型。请参阅 C# 规范,第 7.6.10.4 节:这里的关键点是“最佳常见类型”只能是已经存在的类型之一。正如Damien_The_Unknowner在评论中指出的那样:“正如Lippert先生喜欢指出的那样推论,每当它寻找最佳公共类型时,它只会返回已经存在的类型之一 - 它不会去寻找最衍生的公共祖先。”。
仅仅因为每个数组都可以是一个
object []
并不意味着它应该是。从编译器的角度来看,这将是一个微不足道的最后选择,但我想对于开发人员来说这是一个非常违反直觉的选择。The
new []
notation is for saving you to type an explicit type of the array members (or allowing you to create arrays where its elements have an anonymous type), but its type inference is limited in that all elements must share the same type or be implicitly convertible to a common type shared by at least one member. See C# Specification, section 7.6.10.4:Key point here is that the “best common type” can only be one of the types already present. As Damien_The_Unbeliever pointed out in a comment: “As Mr. Lippert is fond of pointing out around inference, whenever it's looking for a best common type, it will only return one of the types that is already present - it doesn't go hunting for the most-derived common ancestor.”.
Just because every array could be an
object []
doesn't mean it should. From a compiler perspective that'd be a trivial last-resort choice, but a very counter-intuitive one for the developer, I guess.为了扩展 Joey 的答案,请考虑以下示例:
两个类都实现了两个接口(并且还从
object
派生),因此应该为array
推断哪种类型?要回答您的评论,请考虑
A
和B
仅共享一个接口的情况:A
和B
共享object
作为它们的基类,但是对于数组类型推断这一点是没有帮助的,而且几乎没有用处。如果有的话,人们会期望它是IFoo
,因此它会违反原则最少的惊讶。然而,正如我所说明的,这不能始终如一地完成。这里最安全和最一致的行为就是不允许类型推断。
To expand on Joey's answer, consider this example:
Both classes implement both interfaces (and also derive from
object
), so which type should be inferred forarray
?To answer your comment, consider the case where
A
andB
share only one interface:Both
A
andB
shareobject
as their base class, but it'd be unhelpful and mostly useless to infer this for the array type. One would expect it to beIFoo
if anything, so it would violate the principle of least astonishment. However, this cannot be done consistently, as I've illustrated.The safest and most consistent behaviour here is simply not to allow type inference.