【问题标题】:VB.Net IEnumerator(Of Integer, MyClass) not possible?VB.Net IEnumerator(Of Integer, MyClass) 不可能?
【发布时间】:2013-07-06 11:09:12
【问题描述】:

如何返回IEnumeratorSortedDictionary(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


【解决方案1】:
Dim dictionaryTest As New SortedDictionary(Of Integer, MyClass)

答案是替换

Dim enumerator As IEnumerator(Of Integer, MyClass) = dictionaryTest.GetEnumerator() '<- not possible.

Dim enumerator As IEnumerator(Of KeyValuePair(Of Integer, Myclass)) = dictionaryTest.GetEnumerator()

【讨论】:

    猜你喜欢
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 2013-04-07
    相关资源
    最近更新 更多