为什么在.NET数组只能实现IEnumerable而不是IEnumerable的< T>? [英] Why do arrays in .net only implement IEnumerable and not IEnumerable<T>?

查看:156
本文介绍了为什么在.NET数组只能实现IEnumerable而不是IEnumerable的< T>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施自己的ArrayList类,并留下惊讶,当我意识到

I was implementing my own ArrayList class and was left surprised when I realised that

public System.Collections.Generic.IEnumerator<T> GetEnumerator() {
    return _array.GetEnumerator();
}

没有工作。什么是数组不要在.NET中实现IEnumerator的原因是什么?

didn't work. What is the reason arrays don't implement IEnumerator in .NET?

有没有什么解决方法?

感谢

推荐答案

阵列都实施的IEnumerable&LT; T&GT; ,但它是作为的专业知识的CLI部分有数组。许多工具将不再显示此实现。看到言论段开始注意到在文档(文档早期.NET版本:外观为重要块)

Arrays do implement IEnumerable<T>, but it is done as part of the special knowledge the CLI has for arrays. Many tools will not show this implementation. See the paragraph in the remarks starting "Starting with the .NET Framework 2.0" note in the documentation (documentation for earlier .NET versions: look for the "Important" block).

您可以添加一个转换:

return ((IEnumerable<T>)_array).GetEnumerator();

这篇关于为什么在.NET数组只能实现IEnumerable而不是IEnumerable的&LT; T&GT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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