System.Collections.Generic.List(Of T)
实施System.Collections.IEnumerable
和System.Collections.Generic.IEnumerable(Of T)
,每个都有GetEnumerator()
方法(分别):
Function GetEnumerator() As System.Collections.IEnumerator
Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of T)
假设我myList
是System.Collections.Generic.List(Of T)
的一个实例;如果我调用myList.GetEnumerator()
,编译器目标Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of T)
和Function GetEnumerator() As System.Collections.IEnumerator
将从智能感知列表中隐藏。实际上,如果我使用ILSpy查看System.Collections.Generic.List(Of T)
,则后一个重载完全丢失。
我想创建自己的集合类来实现这两个接口,所以我必须这样做:
Class MyItemCollection
Implements System.Collections.IEnumerable,
System.Collections.Generic.IEnumerable(Of Item)
Private _items As New System.Collections.Generic.List(Of Item)
Public Function GetEnumerator() As IEnumerator(Of Item) _
Implements IEnumerable(Of Item).GetEnumerator
Return _items.GetEnumerator()
End Function
Public Function GetEnumerator1() As IEnumerator _
Implements IEnumerable.GetEnumerator
Return _items.GetEnumerator()
End Function
End Class
请注意,我必须使用GetEnumerator
和GetEnumerator1
,因为由于返回类型不同,我无法将它们合并为一个GetEnumerator
方法。如果我遗漏Function GetEnumerator() As System.Collections.IEnumerator
,我会收到编译错误。
System.Collections.Generic.List(Of T)
如何通过而不是来实现这两个重载,如果它实际上是它正在做什么,或者如果没有,如何它确定它会出现哪种超载?如何使用我的VB.NET代码实现此结果?
注意:我认为,正如the_lotus所指出的,这与显式接口实现有关,但在VB.NET中并不像在C#中那样存在。