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

查看:99
本文介绍了为什么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#

我可以创建如下代码:

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) { }
}


b $ b

由于我满足编译器的最低要求(实现 IEnumerable public void Add )这个工作,但显然没有价值。

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

我想知道什么阻止了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在2006年10月发表了一篇题为 什么是集合? p>

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


承认,我们在第一个
版本的框架中使用
System.Collections.ICollection,其中
是没用的。但是当我们在.NET框架2.0中使用
时,我们修复了

System.Collections.Generic.ICollection< 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< T&每次
他们做一个集合,对吧?不是这样。
下面是我们如何使用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< T> 实现 IEnumerable 并具有公共 Add()方法。

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< T> 接口,将只有一个支持

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.

为了说明,我们稍微扩展你的代码:

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天全站免登陆