using System;
using System.Collections.Generic;
public class Example
public static void Main()
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Tyrannosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Mamenchisaurus");
dinosaurs.Add("Brachiosaurus");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Tyrannosaurus");
dinosaurs.Add("Compsognathus");
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
Console.WriteLine(dinosaur);
Console.WriteLine("\nIndexOf(\"Tyrannosaurus\"): {0}",
dinosaurs.IndexOf("Tyrannosaurus"));
Console.WriteLine("\nIndexOf(\"Tyrannosaurus\", 3): {0}",
dinosaurs.IndexOf("Tyrannosaurus", 3));
Console.WriteLine("\nIndexOf(\"Tyrannosaurus\", 2, 2): {0}",
dinosaurs.IndexOf("Tyrannosaurus", 2, 2));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
IndexOf("Tyrannosaurus"): 0
IndexOf("Tyrannosaurus", 3): 5
IndexOf("Tyrannosaurus", 2, 2): -1
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
Dim dinosaurs As New List(Of String)
dinosaurs.Add("Tyrannosaurus")
dinosaurs.Add("Amargasaurus")
dinosaurs.Add("Mamenchisaurus")
dinosaurs.Add("Brachiosaurus")
dinosaurs.Add("Deinonychus")
dinosaurs.Add("Tyrannosaurus")
dinosaurs.Add("Compsognathus")
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Console.WriteLine(vbLf & _
"IndexOf(""Tyrannosaurus""): {0}", _
dinosaurs.IndexOf("Tyrannosaurus"))
Console.WriteLine(vbLf & _
"IndexOf(""Tyrannosaurus"", 3): {0}", _
dinosaurs.IndexOf("Tyrannosaurus", 3))
Console.WriteLine(vbLf & _
"IndexOf(""Tyrannosaurus"", 2, 2): {0}", _
dinosaurs.IndexOf("Tyrannosaurus", 2, 2))
End Sub
End Class
' This code example produces the following output:
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'IndexOf("Tyrannosaurus"): 0
'IndexOf("Tyrannosaurus", 3): 5
'IndexOf("Tyrannosaurus", 2, 2): -1
int IndexOf(T item, int index);
public int IndexOf (T item, int index);
member this.IndexOf : 'T * int -> int
Public Function IndexOf (item As T, index As Integer) As Integer
List<T>
从 开始
index
,在最后一个元素处结束,向前搜索 。
此方法使用 的默认相等比较器
EqualityComparer<T>.Default
确定相等性,即
T
列表中的值类型。
此方法执行线性搜索;因此,此方法是 O (
n
) 操作,其中
n
是从
index
到 末尾的
List<T>
元素数。
public:
int IndexOf(T item, int index, int count);
public int IndexOf (T item, int index, int count);
member this.IndexOf : 'T * int * int -> int
Public Function IndexOf (item As T, index As Integer, count As Integer) As Integer
List<T>
如果
count
大于 0,则向前搜索从 开始
index
,在
index
加
count
减 1 处结束。
此方法使用 的默认相等比较器
EqualityComparer<T>.Default
确定相等性,即
T
列表中的值类型。
此方法执行线性搜索;因此,此方法是 O (
n
) 操作,其中
n
为
count
。
virtual int IndexOf(T item);