清单< INT>测试= {1,2,3} - 它是一个功能或一个错误? [英] List<int> test = {1, 2, 3} - is it a feature or a bug?

查看:128
本文介绍了清单< INT>测试= {1,2,3} - 它是一个功能或一个错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如你所知,这是不允许使用阵列的初始化语法列表。它会给一个编译时错误。例如:

As you know, it is not allowed to use the Array-initialisation syntax with Lists. It will give a compile-time error. Example:

List<int> test = { 1, 2, 3} 
// At compilation the following error is shown:
// Can only use array initializer expressions to assign to array types. 



不过今天我做了以下(很简单):

However today I did the following (very simplified):

class Test
{
     public List<int> Field;
}

List<Test> list = new List<Test>
{
    new Test { Field = { 1, 2, 3 } }
};



上面的代码编译得很好,但在运行时会给出一个对象引用未设置为对象运行时错误。

The code above compiles just fine, but when run it will give a "Object references is not set to an object" run-time error.

我希望这些代码给一个编译时错误。我对你的问题是:为什么没有它,有什么好的理由时,这种情况会正常运行。

I would expect that code to give a compile-time error. My question to you is: Why doesn't it, and are there any good reasons for when such a scenario would run correctly?

这已经使用.NET 3.5测试? ,同时提供.NET和Mono的编译器。

This has been tested using .NET 3.5, both .Net and Mono compilers.

干杯。

推荐答案

我认为这是一个由设计行为。在测试= {1,2,3} 被编译到调用代码添加存储在列表中的方法测试字段。

I think this is a by-design behavior. The Test = { 1, 2, 3 } is compiled into code that calls Add method of the list stored in the Test field.

为什么你要原因的NullReferenceException 测试。如果初始化测试字段到一个新的列表,然后代码将工作:

The reason why you're getting NullReferenceException is that Test is null. If you initialize the Test field to a new list, then the code will work:

class Test {    
  public List<int> Field = new List<int>(); 
}  

// Calls 'Add' method three times to add items to 'Field' list
var t = new Test { Field = { 1, 2, 3 } };



这是很合乎逻辑的 - 如果你写新的List< INT> {...} 然后创建列表的新实例。如果不添加对象构造,它将使用现有的实例(或)。据我所看到的,C#规范不包含任何明确的转换规则,将匹配这个场景,但它提供了一个例子(见栏目7.6.10.3 ):

It is quite logical - if you write new List<int> { ... } then it creates a new instance of list. If you don't add object construction, it will use the existing instance (or null). As far as I can see, the C# spec doesn't contain any explicit translation rule that would match this scenario, but it gives an example (see Section 7.6.10.3):

A 列表<联系与GT; 可以创建和初始化如下:

A List<Contact> can be created and initialized as follows:

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

具有为

var contacts = new List<Contact>();
Contact __c1 = new Contact();
__c1.Name = "Chris Smith";
__c1.PhoneNumbers.Add("206-555-0101");
__c1.PhoneNumbers.Add("425-882-8080");
contacts.Add(__c1);
Contact __c2 = new Contact();
__c2.Name = "Bob Harris";
__c2.PhoneNumbers.Add("650-555-0199");
contacts.Add(__c2);

,其中 __ C1 __ C2 是临时变量是不可见的,交通不便。

where __c1 and __c2 are temporary variables that are otherwise invisible and inaccessible.

这篇关于清单&LT; INT&GT;测试= {1,2,3} - 它是一个功能或一个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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