IEnumerable T提供了两个GetEnumerator方法-它们之间有什么区别? [英] IEnumerable<T> provides two GetEnumerator methods - what is the difference between them?

查看:85
本文介绍了IEnumerable T提供了两个GetEnumerator方法-它们之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我建立IEnumerable<T>接口时,我看到两种GetEnumerator方法:一种返回IEnumerator,另一种返回IEnumerator<T>.我什么时候使用一个或另一个?

When I emplement IEnumerable<T> interface I see two GetEnumerator methods: one returning IEnumerator and other IEnumerator<T>. When would I use one or another?

推荐答案

如果要实现IEnumerable<T>通用接口,则几乎总是必须使用通用GetEnumerator方法-除非将对象显式转换为(非-通用)IEnumerable.

If you are implementing the IEnumerable<T> generic interface, you will pretty much always have to use the generic GetEnumerator method - unless you cast your object explicitly to (non-generic) IEnumerable.

原因是与不支持泛型的.NET 1.0/1.1向后兼容.

The reason is backwards compatability with .NET 1.0/1.1 which didn't support generics.

这篇关于IEnumerable T提供了两个GetEnumerator方法-它们之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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