为什么编译器不能推断对象数组类型? [英] Why Compiler cannot infer the object array type?

查看:91
本文介绍了为什么编译器不能推断对象数组类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么这是有效的:

 对象[]数组= {唧唧歪歪。,23,真正的};

但这些都不是:

  VAR阵列= {唧唧歪歪。,23,真正的};
VAR数组2 =新[] {唧唧歪歪。,23,真正的};

在第二个例子,为什么按照数组初始化值编译器不能推断出数组类型?这似乎很容易做到,特别是相对于一般类型inference.In为了定义一个对象数组为什么我必须指定数组类型明确?

  VAR阵列=新的对象[] {唧唧歪歪。,23,真正的};


解决方案

由于数组中的类型不特定对象 - 他们是3种不同类型的都对象的子类。

如果您使用可以在阵列中推断出特定类型,编译器会推断类型。例如,这是好的:

  VAR ARR =新[] {3,4,5}; //将正确推断INT []

请注意,这是明确的C#语言规范,其中规定8.5.1叫出来,对于 VAR 是受到以下限制:


  • 的局部变量声明不能包括多个局部变量声明符。

  • 的局部变量声明必须包含一个局部变量初始化。

  • 的局部变量初始化必须是前pression。

  • 的初始化前pression必须有一个编译时类型。

  • 的初始化前pression不能引用声明的变量本身

在阵列的情况下,存在指定的示例:

 变种Y = {1,2,3}; //错误,数组初始化不允许

对于新[] {唧唧歪歪。,23,真正}; 的例子,这是在7.6.10叫出来。在那里,这个例子:

 变种D =新[] {1,一,2,二}; //错误

据说是一个错误,因为:


  

最后一个前pression导致编译时错误,因为没有诚信,也不字符串隐式转换为其他的,所以没有最好常见的类型。显式类型的数组创建前pression必须在这种情况下使用,例如,指定的类型为对象[]。可替代地,这些元件中的一个可被转换为一个共同的基类型,则这将成为推断元素类型


I am wondering why this is valid :

object[] array = {"bla bla bla..", 23, true};

But these are not:

var array = {"bla bla bla..", 23, true };
var array2 = new [] {"bla bla bla..", 23, true };

In the second example why compiler cannot infer the array type according to values in the array initializer? It seems really easy to do, especially as compared to generic type inference.In order to define an object array why do I have to specify the array type explicitly ?

var array = new object[] { "bla bla bla..", 23, true };

解决方案

Because the types in the array aren't specific object - they are 3 different types that are all subclasses of object.

If you use a specific type that can be inferred in your array, the compiler will infer the type. For example, this is fine:

var arr = new[] {3, 4, 5}; // Will correctly infer int[]

Note that this is explicitly called out in 8.5.1 of the C# language spec, which states that for var is subject to the following restrictions:

  • The local-variable-declaration cannot include multiple local-variable-declarators.
  • The local-variable-declarator must include a local-variable-initializer.
  • The local-variable-initializer must be an expression.
  • The initializer expression must have a compile-time type.
  • The initializer expression cannot refer to the declared variable itself

In the case of arrays, there is an example specified:

var y = {1, 2, 3};   // Error, array initializer not permitted

As for the new [] {"bla bla bla..", 23, true }; example, this is called out in 7.6.10. There, this example:

var d = new[] { 1, "one", 2, "two" }; // Error

Is said to be an error because:

The last expression causes a compile-time error because neither int nor string is implicitly convertible to the other, and so there is no best common type. An explicitly typed array creation expression must be used in this case, for example specifying the type to be object[]. Alternatively, one of the elements can be cast to a common base type, which would then become the inferred element type

这篇关于为什么编译器不能推断对象数组类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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