什么是这个新的[]的简写? [英] What is this new[] a shorthand for?

查看:177
本文介绍了什么是这个新的[]的简写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找到被认为有什么新的[]是任何文件。从它下面的例子似乎是一个对象数组速记

  VAR JSON =新[] {
新的对象[ ] {20君2008,200},
新的对象[] {20君2009,250}
};


解决方案

这些都是隐式类型数组



请参阅的 C#3.0规范




数组创建表​​达式的语法(§7.5.10.2)延长至
都支持隐式类型的数组创建表​​达式:
数组创建表​​达式:... 新[]数组初始化



在一个隐式类型的数组创建表​​达式中,
数组实例的类型是从数组
初始化中指定的元素推断。具体来说,在类型的数组初始化的
表达形成的集必须包含一个类型为
这在设定每种类型的隐式转换,如果该类型
是不空型时,将创建该类型的阵列。如果恰好一个
型无法推断,或者如果推断类型是空型,发生
编译时错误



以下是隐式类型的数组创建
表达式的例子:

  VAR一个=新的[] {1,10,100, 1000}; // INT [] 
变种B =新[] {1,1.5,2,2.5}; //双[]
变种C =新[] {你好,空,世界}; //字符串[]
变种D =新[] {1,一,2,二}; //错误

最后一个表达式将导致编译时错误,因为无论INT
,也不串隐式转换为其他的。显式类型
数组创建表​​达式必须在这种情况下使用,例如
指定类型为object []。另外,元素
中的一个可以转换为一个共同的基本类型,那么这将成为
推断元素类型。



隐式类型数组创建表达式可以用
匿名对象初始化结合起来,创建匿名类型化的数据
结构。例如:

  VAR接触=新的[] {
新{
NAME =克里斯·史密斯 ,
PHONENUMBERS =新[] {206-555-0101,425-882-8080}
},
新的{
NAME =鲍勃·哈里斯
PHONENUMBERS =新[] {650-555-0199}
}
};



I can't seem to find any documentation on what new[] is supposed to be. From the example below it seems to be an object array shorthand

var json = new[] {
            new object[] {"20-Jun-2008", 200 },
            new object[] {"20-Jun-2009", 250 }
        };

解决方案

These are implicitly typed arrays.

See C# 3.0 specifications.

The syntax of array creation expressions (§7.5.10.2) is extended to support implicitly typed array creation expressions: array-creation-expression: ... new [ ] array-initializer

In an implicitly typed array creation expression, the type of the array instance is inferred from the elements specified in the array initializer. Specifically, the set formed by the types of the expressions in the array initializer must contain exactly one type to which each type in the set is implicitly convertible, and if that type is not the null type, an array of that type is created. If exactly one type cannot be inferred, or if the inferred type is the null type, a compile-time error occurs.

The following are examples of implicitly typed array creation expressions:

var a = new[] { 1, 10, 100, 1000 };            // int[]
var b = new[] { 1, 1.5, 2, 2.5 };            // double[]
var c = new[] { "hello", null, "world" };      // string[]
var d = new[] { 1, "one", 2, "two" };         // Error

The last expression causes a compile-time error because neither int nor string is implicitly convertible to the other. 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.

Implicitly typed array creation expressions can be combined with anonymous object initializers to create anonymously typed data structures. For example:

var contacts = new[] {
   new {
      Name = "Chris Smith",
      PhoneNumbers = new[] { "206-555-0101", "425-882-8080" }
   },
   new {
      Name = "Bob Harris",
      PhoneNumbers = new[] { "650-555-0199" }
   }
};

这篇关于什么是这个新的[]的简写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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