IEnumerable和IEnumerable的℃之间的差异; T&GT ;? [英] Difference between IEnumerable and IEnumerable<T>?

查看:150
本文介绍了IEnumerable和IEnumerable的℃之间的差异; T&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是的IEnumerable之间的差异的IEnumerable< T>

我见过的同时实现这些接口的很多框架类,所以我想知道通过实施两下合而为一得到什么好处呢?

I've seen many framework classes implementing both these interfaces, therefore I would like to know what advantages one get by implementing both?

请看看他们是如何被定义:

Please have a look how they've been defined:

public interface IEnumerable
{
    [DispId(-4)]
    IEnumerator GetEnumerator();
}
public interface IEnumerable<T> : IEnumerable
{
    IEnumerator<T> GetEnumerator();
}

正如我们看到的,的IEnumerable< T> 的IEnumerable ,这意味着无论的IEnumerable 的IEnumerable< T> ; 继承的,那么为什么我们同时实现,而不是仅仅的IEnumerable< T> ?正在实施的IEnumerable< T> 不够

As we see, IEnumerable<T> derives from IEnumerable, that means whatever IEnumerable has, IEnumerable<T> inherits, then why do we implement both instead of just IEnumerable<T>? Is implementing IEnumerable<T> not enough?

同样,也有其他类似的对:

Likewise, there are other similar pairs:


  • 的IList 的IList< T>

  • 的ICollection 的ICollection< T>

  • IList and IList<T>
  • ICollection and ICollection<T>

我想知道这些孩子的。

推荐答案

基本上非泛型接口是先在.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<的条件; 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< T> 做的的延长的IList 的ICollection< T> 做的的延长的ICollection 。这是因为它是类型安全的少这样做......而任何通用的迭代可以被看作是一个非泛型迭代器由于(可能拳)的任何值转换为对象的IList 的ICollection 允许值是的添加应用于收集;它没有任何意义添加(说)的字符串到的IList< 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< T> 是这样我们就可以在一个类型安全的方式进行迭代,并围绕传播该信息。如果我返回的IEnumerable<串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< T> 不延长的IEnumerable ,但任何想要使用的的IEnumerable<码; T> 无法调入接受的方法的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.

这篇关于IEnumerable和IEnumerable的℃之间的差异; T&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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