VB.Net IEnumerator(整数,MyClass)不可能?
如何返回VB.Net IEnumerator(整数,MyClass)不可能?,vb.net,class,typeof,enumerator,ienumerator,Vb.net,Class,Typeof,Enumerator,Ienumerator,如何返回SortedDictionary(整数,MyClass)的IEnumerator 我有这个 Dim dictionaryTest As New SortedDictionary(Of Integer, MyClass) Dim enumerator As IEnumerator(Of Integer, MyClass) = dictionaryTest.GetEnumerator() '<- not possible. 在IDE中生成错误 “System.Collections.
SortedDictionary(整数,MyClass)的IEnumerator
我有这个
Dim dictionaryTest As New SortedDictionary(Of Integer, MyClass)
Dim enumerator As IEnumerator(Of Integer, MyClass) = dictionaryTest.GetEnumerator() '<- not possible.
在IDE中生成错误
“System.Collections.IEnumerator”没有类型参数,因此不能有类型参数。
Dim dictionaryTest As New SortedDictionary(Of Integer, MyClass)
答案是替换
Dim enumerator As IEnumerator(Of Integer, MyClass) = dictionaryTest.GetEnumerator() '<- not possible.
您是否导入了System.Collections.Generic
命名空间?另外,我相信它实际上是一个IEnumerator(属于KeyValuePair(属于Integer,Myclass))
工作得非常完美。我缺少的是KeyValuePair(…)的
。奇怪的是,为什么我不必对SortedDictionary
使用KeyValuePair
,但我需要它来处理GetEnumerator()
。我把它放在IEnumerator
上,它似乎也能工作,但还没有导入系统.Collections.Generic
仍然没有编译器错误或警告。我想,我不需要那个库,导入系统。集合
可以工作,但可能我正在导入所有东西,不确定有两个IEnumerable
接口-通用IEnumerable(Of t)
和非通用IEnumerable
。IIRC前者在System.Collections.Generic
中,后者在System.Collections中
至于KeyValuePair
我不确定为什么在顶部示例中不需要它,VB一定在幕后做了一些可爱的事情。我很确定这个样本在C语言中不起作用。
Dim enumerator As IEnumerator(Of Integer, MyClass) = dictionaryTest.GetEnumerator() '<- not possible.
Dim enumerator As IEnumerator(Of KeyValuePair(Of Integer, Myclass)) = dictionaryTest.GetEnumerator()