Tim*_*ter 25
您可以使用FindIndex查找泛型List中对象的索引:这是获取对象索引的最灵活方法.
Dim list As New List(Of Object)
Const myApple = "Apple111"
For i = 0 To 1000
List.Add("Apple" & i)
Next
Dim indexOfMyApple = list.FindIndex(Function(apple) myApple.Equals(apple))
Run Code Online (Sandbox Code Playgroud)
但是,如果您只想通过DefaultEqualityComparer在List中查找对象,则IndexOf方法甚至更简单,更直接:
Dim indexOfMyApple = list.IndexOf(myApple)
Run Code Online (Sandbox Code Playgroud)
IndexOf
如果你不知道它是什么类型,你也可以使用它,.NET将使用Equals来确定两个对象是否相等(应该被覆盖以不仅比较引用).