且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

IEnumerable和IEnumerable的℃之间的差异; T> ;?

更新时间:2021-07-30 14:55:52

基本上非泛型接口是先在.NET 1.0和1.1。然后,当.NET 2.0出来了,一般等价物就出来了。生活本来简单了很多,如果仿制药已经使它成为.NET 1.0:)

Basically the nongeneric interfaces came first, in .NET 1.0 and 1.1. Then when .NET 2.0 came out, the generic equivalents came out. Life would have been a lot simpler if generics had made it into .NET 1.0 :)

在实施只有的IEnumerable&LT的条件; T> ,而不是两个 - 你基本上有无的实施两者,你必须使用显式接口实现过,因为这两个定义参数的的GetEnumerator 方法。由于的IEnumerator< T> 扩展的IEnumerator 过,它通常是这样的:

In terms of implementing "only" IEnumerable<T> instead of both - you basically have to implement both, and you have to use explicit interface implementation too, given that both define a parameterless GetEnumerator method. As IEnumerator<T> extends IEnumerator too, it's normally something like this:

public IEnumerator<T> GetEnumerator()
{
    // Return real iterator
}

// Explicit implementation of nongeneric interface
IEnumerator IEnumerable.GetEnumerator()
{
    // Delegate to the generic implementation
    return GetEnumerator();
}

在另一方面,在C#2中引入迭代块(与收益回报率等),你很少需要完全采用手工实现这些东西,幸运的。你可能需要写类似上面,然后用收益回报率的GetEnumerator 方法。

On the other hand, with the iterator blocks introduced in C# 2 (with yield return etc) you rarely need to implement these things entirely by hand, fortunately. You may need to write something like the above, and then use yield return in the GetEnumerator method.

注意的IList&LT; T&GT; 做的的延长的IList 的ICollection&LT; T&GT; 做的的延长的ICollection 。这是因为它是类型安全的少这样做......而任何通用的迭代可以被看作是一个非泛型迭代器由于(可能拳)的任何值转换为对象的IList 的ICollection 允许值是的添加应用于收集;它没有任何意义添加(说)的字符串到的IList&LT; INT方式>

Note that IList<T> does not extend IList, and ICollection<T> does not extend ICollection. That's because it's less type-safe to do so... whereas any generic iterator can be seen as a nongeneric iterator due to the (potentially boxing) conversion of any value to object, IList and ICollection allow values to be added to the collection; and it doesn't make sense to add (say) a string to an IList<int>.

编辑:我们为什么需要理由的IEnumerable&LT; T&GT; 是这样我们就可以在一个类型安全的方式进行迭代,并围绕传播该信息。如果我返回的IEnumerable&LT;串GT; 给你,你知道你可以安全地假设从它返回的是一个字符串引用或空的一切。随着的IEnumerable ,我们必须有效地投(通常隐含在的foreach 语句),这是从序列返回的每个元素,因为的IEnumerator 只是类型的对象电流属性>。至于为什么我们的还是的需要的IEnumerable - 因为旧的接口永远不会消失,基本上是这样。 。还有的使用太多现有的代码

The reason why we need IEnumerable<T> is so that we can iterate in a type-safe way, and propagate that information around. If I return an IEnumerable<string> to you, you know that you can safely assume everything returned from it will be a string reference or null. With IEnumerable, we had to effectively cast (often implicitly in a foreach statement) each element that was returned from the sequence, because the Current property of IEnumerator is just of type object. As for why we still need IEnumerable - because old interfaces never go away, basically. There's too much existing code using it.

这本来是可能的的IEnumerable&LT; T&GT; 不延长的IEnumerable ,但任何想要使用的的IEnumerable&LT码; T&GT; 无法调入接受的方法的IEnumerable - 而且有很多像从.NET 1.1和1.0方法

It would have been possible for IEnumerable<T> not to extend IEnumerable, but then any code wanting to make use of an IEnumerable<T> couldn't call into a method accepting IEnumerable - and there were a lot of methods like that from .NET 1.1 and 1.0.