从数组中获取通用枚举器 [英] obtain generic enumerator from an array
本文介绍了从数组中获取通用枚举器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 C# 中,如何从给定的数组中获取通用枚举数?
In C#, how does one obtain a generic enumerator from a given array?
在下面的代码中,MyArray
是一个 MyType
对象的数组.我想以显示的方式获取 MyIEnumerator
,但似乎我获得了一个空的枚举数(尽管我已经确认 MyArray.Length > 0
).
In the code below, MyArray
is an array of MyType
objects. I'd like to obtain MyIEnumerator
in the fashion shown,
but it seems that I obtain an empty enumerator (although I've confirmed that MyArray.Length > 0
).
MyType[] MyArray = ... ;
IEnumerator<MyType> MyIEnumerator = MyArray.GetEnumerator() as IEnumerator<MyType>;
推荐答案
适用于 2.0+:
((IEnumerable<MyType>)myArray).GetEnumerator()
适用于 3.5+(花哨的 LINQy,效率稍低):
Works on 3.5+ (fancy LINQy, a bit less efficient):
myArray.Cast<MyType>().GetEnumerator() // returns IEnumerator<MyType>
这篇关于从数组中获取通用枚举器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文