简体   繁体   English

为什么集合初始化表达式需要实现IEnumerable?

[英]Why does a collection initializer expression require IEnumerable to be implemented?

Why does this generate a compiler error: 为什么会产生编译器错误:

class X { public void Add(string str) { Console.WriteLine(str); } }

static class Program
{
    static void Main()
    {
        // error CS1922: Cannot initialize type 'X' with a collection initializer
        // because it does not implement 'System.Collections.IEnumerable'
        var x = new X { "string" };
    }
}

but this doesn't: 但这不是:

class X : IEnumerable
{
    public void Add(string str) { Console.WriteLine(str); }
    IEnumerator IEnumerable.GetEnumerator()
    {
        // Try to blow up horribly!
        throw new NotImplementedException();
    }
}

static class Program
{
    static void Main()
    {
        // prints “string” and doesn’t throw
        var x = new X { "string" };
    }
}

What is the reason for restricting collection initializers — which are syntactic sugar for a call to an Add method — to classes that implement an interface which doesn't have an Add method and which isn't used? 什么是限制的集合初始化的原因-这是一个调用语法糖Add方法-以实现其不具有的接口类Add方法和不使用?

An object initializer doesn't; 对象初始化器不会; a collection initializer does. 集合初始化程序。 It's so that it's applied to classes which really represent collections, rather than just arbitrary ones which have an Add method. 这样它就可以应用于真正代表集合的类,而不仅仅是具有Add方法的任意类。 I have to admit that every so often I've "implemented" IEnumerable explicitly, just to allow collection initializers - but thrown a NotImplementedException from GetEnumerator() . 我必须承认,我经常明确地“实现” IEnumerable ,只是为了允许集合初始化器 - 但是从GetEnumerator()抛出了一个NotImplementedException

Note that early in C# 3's development, collection initializers had to implement ICollection<T> , but that was found to be too restrictive. 请注意,在C#3开发的早期,集合初始化程序必须实现ICollection<T> ,但发现它过于严格。 Mads Torgersen blogged about this change , and the reason behind requiring IEnumerable , back in 2006. Mads Torgersen在2006年发表了关于这一变化的博客 ,以及要求IEnumerable背后的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用集合初始化程序初始化类型,因为它没有实现ienumerable - cannot initialize type with a collection initializer because it does not implement ienumerable 为什么IEnumerable变量的项在指向IEnumerable时无法更新,但在指向实现的集合时有效 - Why items of an IEnumerable variable cannot be updated when it is pointing to an IEnumerable but works when pointing to an implemented collection 采集 <T> :为什么同时实现IEnumerable和IEnumerable <T> ? - Collection<T>: why does it implement both IEnumerable and IEnumerable<T>? C# JSON - 错误:无法使用集合初始值设定项初始化类型(未实现“System.Collection.IEnumerable”) - C# JSON - Error: Cannot initialize type with collection initializer (does not implement 'System.Collection.IEnumerable') 为什么集合初始值设定项不与表达式body属性一起使用? - Why collection initializer is not working together with expression body property? 为什么IEnumerable <T> 需要调用ToList才能更新listview? - Why does an IEnumerable<T> require a call to ToList to update the listview? 无法使用集合初始值设定项初始化类型x,因为它未实现“ System.Collections.IEnumerable” - Cannot initialize type x with a collection initializer because it does not implement 'System.Collections.IEnumerable' 无法使用集合初始值设定项实现类型,因为它没有实现“System.Collections.IEnumerable” - Cannot implement type with a collection initializer because it does not implement 'System.Collections.IEnumerable' 无法使用集合初始化程序初始化类型“ x”,因为它未实现“ System.Collections.IEnumerable” - Cannot initialize type “x” with a collection initializer because it does not implement 'System.Collections.IEnumerable' 无法使用集合初始化程序初始化类型“ PeopleModel”,因为它未实现“ System.Collections.IEnumerable” - Cannot initialize type 'PeopleModel' with a collection initializer because it does not implement 'System.Collections.IEnumerable'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM