为什么C#集合初始化以这种方式工作? [英] Why do C# collection initializers work this way?

查看:125
本文介绍了为什么C#集合初始化以这种方式工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看着C#集合初始化,发现执行是非常务实的,但也很不像在C#别的

I was looking at C# collection initializers and found the implementation to be very pragmatic but also very unlike anything else in C#

我能够创建code是这样的:

I am able to create code like this:

using System;
using System.Collections;

class Program
{
    static void Main()
    {
    	Test test = new Test { 1, 2, 3 };
    }
}

class Test : IEnumerable
{
    public IEnumerator GetEnumerator()
    {
    	throw new NotImplementedException();
    }

    public void Add(int i) { }
}

既然我已经满足了编译器的最低要求(实施的IEnumerable 公共无效添加)该作品但显然没有价值。

Since I have satisfied the minimum requirements for the compiler (implemented IEnumerable and a public void Add) this works but obviously has no value.

我想知道什么prevented C#的团队创造一个更加严格的一套要求?换句话说,为什么,为了让这个语法来编译,该编译器不需要该类型实施的ICollection ?这似乎更在其他C#特性的精神。

I was wondering what prevented the C# team from creating a more strict set of requirements? In other words why, in order for this syntax to compile, does the compiler not require that the type implement ICollection? That seems more in the spirit of other C# features.

推荐答案

您的观察是对现货 - 事实上,它反映一个接的Mads托格森,微软C#语言PM

Your observation is spot on - in fact, it mirrors one made by Mads Torgersen, a Microsoft C# Language PM.

的Mads 2006年10月关于这个问题的标题发了一个帖子的什么是一家集 的他在其中写道:

Mads made a post in October 2006 on this subject titled What Is a Collection? in which he wrote:

录取,我们在第一次考砸了
  与框架的版本
  System.Collections.ICollection,这
  基本是没用的。但是,我们固定起来
  pretty以及仿制药的时候来了
  在.NET Framework 2.0中:
  &了System.Collections.Generic.ICollection LT; T>
  允许您添加和删除元素,
  列举出来,数一数,并检查
  入会。

Admitted, we blew it in the first version of the framework with System.Collections.ICollection, which is next to useless. But we fixed it up pretty well when generics came along in .NET framework 2.0: System.Collections.Generic.ICollection<T> lets you Add and Remove elements, enumerate them, Count them and check for membership.

显然,从那时起,大家就
  实现ICollection的&LT; T&GT;每次
  他们提出了一个集合,对不对?事实并非如此。
  下面是我们如何使用LINQ学习
  什么藏品真的是和
  怎么说让我们改变我们的语言
  设计在C#3.0。

Obviously from then on, everyone would implement ICollection<T> every time they make a collection, right? Not so. Here is how we used LINQ to learn about what collections really are, and how that made us change our language design in C# 3.0.

事实证明,只有14个的ICollection&LT的实现; T&GT; 的框架,但189类实现的IEnumerable 和具有公共添加()方法。

It turns out that there are only 14 implementations of ICollection<T> in the framework, but 189 classes that implement IEnumerable and have a public Add() method.

有一个隐藏的利益以这种方式 - 如果他们基于其上的的ICollection&LT; T&GT; 接口,就不会有只有一个支持添加()方法。

There's a hidden benefit to this approach - if they had based it on the ICollection<T> interface, there would have been exactly one supported Add() method.

在此相反,他们确实采取的做法意味着,对于集合初始化刚刚形成集的参数为添加()方法。

In contrast, the approach they did take means that the initializers for the collection just form sets of arguments for the Add() methods.

要说明这一点,让我们来扩展你的code稍微:

To illustrate, let's extend your code slightly:

class Test : IEnumerable
{
    public IEnumerator GetEnumerator()
    {
        throw new NotImplementedException();
    }

    public void Add(int i) { }

    public void Add(int i, string s) { }
}

您现在可以这样写:

class Program
{
    static void Main()
    {
        Test test 
            = new Test 
            {
                1, 
                { 2, "two" },
                3 
            };
    }
}

这篇关于为什么C#集合初始化以这种方式工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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