【发布时间】:2013-07-06 11:09:12
【问题描述】:
如何返回IEnumerator 的SortedDictionary(Of Integer, MyClass)
我有这个。
Dim dictionaryTest As New SortedDictionary(Of Integer, MyClass)
Dim enumerator As IEnumerator(Of Integer, MyClass) = dictionaryTest.GetEnumerator() '<- not possible.
我想要一个类似的功能
public function getTestEnumerator() as IEnumerator(Of Integer, MyClass)
return dictionaryTest.GetEnumerator()
end function
IDE 产生错误'System.Collections.IEnumerator' has no type parameters and so cannot have type arguments.
【问题讨论】:
-
您是否导入了
System.Collections.Generic命名空间?我也相信它实际上是一个IEnumerator(Of KeyValuePair(Of Integer, Myclass)) -
效果很好,我错过了
Of KeyValuePair(...)。奇怪为什么我不必将KeyValuePair用于SortedDictionary,但我需要它用于GetEnumerator()。我只留下了IEnumerator似乎也可以工作 -
尚未导入
System.Collections.Generic仍然没有编译器错误或警告。我认为不需要那个库,Imports System.Collections有效,但也许我不确定导入所有内容 -
有两个
IEnumerable接口——通用IEnumerable(Of T)和非通用IEnumerable。 IIRC 前者在System.Collections.Generic,后者在System.Collections -
至于
KeyValuePair,我不知道为什么您不需要在顶级示例中使用它,VB 肯定在幕后做了一些可爱的事情。我很确定该示例不会在 C# 中工作。
标签: vb.net class typeof enumerator ienumerator