public:
 static int IndexOf(Array ^ array, System::Object ^ value);
public static int IndexOf (Array array, object value);
public static int IndexOf (Array array, object? value);
static member IndexOf : Array * obj -> int
Public Shared Function IndexOf (array As Array, value As Object) As Integer
// Create a string array with 3 elements having the same value. array<String^>^ strings = { "the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog", "in", "the", "barn" }; // Display the elements of the array. Console::WriteLine("The array contains the following values:"); for (int i = strings->GetLowerBound(0); i <= strings->GetUpperBound(0); i++) Console::WriteLine(" [{0,2}]: {1}", i, strings[i]); // Search for the first occurrence of the duplicated value. String^ searchString = "the"; int index = Array::IndexOf(strings, searchString); Console::WriteLine("The first occurrence of \"{0}\" is at index {1}.", searchString, index); // Search for the first occurrence of the duplicated value in the last section of the array. index = Array::IndexOf( strings, searchString, 4); Console::WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.", searchString, index); // Search for the first occurrence of the duplicated value in a section of the array. int position = index + 1; index = Array::IndexOf(strings, searchString, position, strings->GetUpperBound(0) - position + 1); Console::WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.", searchString, position, strings->GetUpperBound(0), index); // The example displays the following output: // The array contains the following values: // [ 0]: the // [ 1]: quick // [ 2]: brown // [ 3]: fox // [ 4]: jumps // [ 5]: over // [ 6]: the // [ 7]: lazy // [ 8]: dog // [ 9]: in // [10]: the // [11]: barn // The first occurrence of "the" is at index 0. // The first occurrence of "the" between index 4 and the end is at index 6. // The first occurrence of "the" between index 7 and index 11 is at index 10.
// Create a string array with 3 elements having the same value.
let strings = 
    [| "the"; "quick"; "brown"; "fox"; "jumps"; "over"
       "the"; "lazy"; "dog"; "in"; "the"; "barn" |]
// Display the elements of the array.
printfn "The array contains the following values:"
for i = strings.GetLowerBound 0 to strings.GetUpperBound 0 do
    printfn $"   [{i,2}]: {strings[i]}"
// Search for the first occurrence of the duplicated value.
let searchString = "the"
let index = Array.IndexOf(strings, searchString)
printfn $"The first occurrence of \"{searchString}\" is at index {index}."
// Search for the first occurrence of the duplicated value in the last section of the array.
let index = Array.IndexOf(strings, searchString, 4)
printfn $"The first occurrence of \"{searchString}\" between index 4 and the end is at index {index}."
// Search for the first occurrence of the duplicated value in a section of the array.
let position = index + 1
let index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound 0 - position + 1)
printfn $"The first occurrence of \"{searchString}\" between index {position} and index {strings.GetUpperBound 0} is at index {index}."
// The example displays the following output:
//    The array contains the following values:
//       [ 0]: the
//       [ 1]: quick
//       [ 2]: brown
//       [ 3]: fox
//       [ 4]: jumps
//       [ 5]: over
//       [ 6]: the
//       [ 7]: lazy
//       [ 8]: dog
//       [ 9]: in
//       [10]: the
//       [11]: barn
//    The first occurrence of "the" is at index 0.
//    The first occurrence of "the" between index 4 and the end is at index 6.
//    The first occurrence of "the" between index 7 and index 11 is at index 10.
// Create a string array with 3 elements having the same value.
String[] strings = { "the", "quick", "brown", "fox", "jumps",
                     "over", "the", "lazy", "dog", "in", "the",
                     "barn" };
// Display the elements of the array.
Console.WriteLine("The array contains the following values:");
for (int i = strings.GetLowerBound(0); i <= strings.GetUpperBound(0); i++)
   Console.WriteLine("   [{0,2}]: {1}", i, strings[i]);
// Search for the first occurrence of the duplicated value.
string searchString = "the";
int index = Array.IndexOf(strings, searchString);
Console.WriteLine("The first occurrence of \"{0}\" is at index {1}.",
                  searchString, index);
// Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4);
Console.WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
                  searchString, index);
// Search for the first occurrence of the duplicated value in a section of the array.
int position = index + 1;
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1);
Console.WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
                  searchString, position, strings.GetUpperBound(0), index);
// The example displays the following output:
//    The array contains the following values:
//       [ 0]: the
//       [ 1]: quick
//       [ 2]: brown
//       [ 3]: fox
//       [ 4]: jumps
//       [ 5]: over
//       [ 6]: the
//       [ 7]: lazy
//       [ 8]: dog
//       [ 9]: in
//       [10]: the
//       [11]: barn
//    The first occurrence of "the" is at index 0.
//    The first occurrence of "the" between index 4 and the end is at index 6.
//    The first occurrence of "the" between index 7 and index 11 is at index 10.
Public Module Example
   Public Sub Main()
      ' Create a string array with 3 elements having the same value.
      Dim strings() As String = { "the", "quick", "brown", "fox",
                                  "jumps", "over", "the", "lazy",
                                  "dog", "in", "the", "barn" }
      ' Display the values of the array.
      Console.WriteLine("The array contains the following values:")
      For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
         Console.WriteLine("   [{0,2}]: {1}", i, strings(i))
      ' Search for the first occurrence of the duplicated value.
      Dim searchString As String = "the"
      Dim index As Integer = Array.IndexOf(strings, searchString)
      Console.WriteLine("The first occurrence of ""{0}"" is at index {1}.",
                        searchString, index)
      ' Search for the first occurrence of the duplicated value in the last section of the array.
      index = Array.IndexOf(strings, searchString, 4)
      Console.WriteLine("The first occurrence of ""{0}"" between index 4 and the end is at index {1}.",
                        searchString, index)
      ' Search for the first occurrence of the duplicated value in a section of the array.
       Dim position As Integer = index + 1
       index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1)
       Console.WriteLine("The first occurrence of ""{0}"" between index {1} and index {2} is at index {3}.",
                         searchString, position, strings.GetUpperBound(0), index)
    End Sub
End Module
' The example displays the following output:
'    The array contains the following values:
'       [ 0]: the
'       [ 1]: quick
'       [ 2]: brown
'       [ 3]: fox
'       [ 4]: jumps
'       [ 5]: over
'       [ 6]: the
'       [ 7]: lazy
'       [ 8]: dog
'       [ 9]: in
'       [10]: the
'       [11]: barn
'    The first occurrence of "the" is at index 0.
'    The first occurrence of "the" between index 4 and the end is at index 6.
'    The first occurrence of "the" between index 7 and index 11 is at index 10.
    	

這個方法會搜尋一維陣列 value 的所有專案。 若要判斷 value 是否存在於 中 array ,方法會呼叫每個元素的 Equals 方法,直到找到相符專案為止,執行相等比較。 這表示,如果專案覆寫 Object.Equals(Object) 方法,則會呼叫該覆寫。

因為大部分陣列的下限為零,所以如果 value 找不到,這個方法通常會傳回 -1。 在罕見的情況下,陣列的下限等於 Int32.MinValue (0x80000000) 且 value 找不到,此方法會 Int32.MaxValue 傳回 (0x7FFFFFFF) 。

這個方法是 O (n) 作業,其中 nLengtharray

public:
 static int IndexOf(Array ^ array, System::Object ^ value, int startIndex);
public static int IndexOf (Array array, object value, int startIndex);
public static int IndexOf (Array array, object? value, int startIndex);
static member IndexOf : Array * obj * int -> int
Public Shared Function IndexOf (array As Array, value As Object, startIndex As Integer) As Integer
// Create a string array with 3 elements having the same value. array<String^>^ strings = { "the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog", "in", "the", "barn" }; // Display the elements of the array. Console::WriteLine("The array contains the following values:"); for (int i = strings->GetLowerBound(0); i <= strings->GetUpperBound(0); i++) Console::WriteLine(" [{0,2}]: {1}", i, strings[i]); // Search for the first occurrence of the duplicated value. String^ searchString = "the"; int index = Array::IndexOf(strings, searchString); Console::WriteLine("The first occurrence of \"{0}\" is at index {1}.", searchString, index); // Search for the first occurrence of the duplicated value in the last section of the array. index = Array::IndexOf( strings, searchString, 4); Console::WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.", searchString, index); // Search for the first occurrence of the duplicated value in a section of the array. int position = index + 1; index = Array::IndexOf(strings, searchString, position, strings->GetUpperBound(0) - position + 1); Console::WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.", searchString, position, strings->GetUpperBound(0), index); // The example displays the following output: // The array contains the following values: // [ 0]: the // [ 1]: quick // [ 2]: brown // [ 3]: fox // [ 4]: jumps // [ 5]: over // [ 6]: the // [ 7]: lazy // [ 8]: dog // [ 9]: in // [10]: the // [11]: barn // The first occurrence of "the" is at index 0. // The first occurrence of "the" between index 4 and the end is at index 6. // The first occurrence of "the" between index 7 and index 11 is at index 10.
// Create a string array with 3 elements having the same value.
let strings = 
    [| "the"; "quick"; "brown"; "fox"; "jumps"; "over"
       "the"; "lazy"; "dog"; "in"; "the"; "barn" |]
// Display the elements of the array.
printfn "The array contains the following values:"
for i = strings.GetLowerBound 0 to strings.GetUpperBound 0 do
    printfn $"   [{i,2}]: {strings[i]}"
// Search for the first occurrence of the duplicated value.
let searchString = "the"
let index = Array.IndexOf(strings, searchString)
printfn $"The first occurrence of \"{searchString}\" is at index {index}."
// Search for the first occurrence of the duplicated value in the last section of the array.
let index = Array.IndexOf(strings, searchString, 4)
printfn $"The first occurrence of \"{searchString}\" between index 4 and the end is at index {index}."
// Search for the first occurrence of the duplicated value in a section of the array.
let position = index + 1
let index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound 0 - position + 1)
printfn $"The first occurrence of \"{searchString}\" between index {position} and index {strings.GetUpperBound 0} is at index {index}."
// The example displays the following output:
//    The array contains the following values:
//       [ 0]: the
//       [ 1]: quick
//       [ 2]: brown
//       [ 3]: fox
//       [ 4]: jumps
//       [ 5]: over
//       [ 6]: the
//       [ 7]: lazy
//       [ 8]: dog
//       [ 9]: in
//       [10]: the
//       [11]: barn
//    The first occurrence of "the" is at index 0.
//    The first occurrence of "the" between index 4 and the end is at index 6.
//    The first occurrence of "the" between index 7 and index 11 is at index 10.
// Create a string array with 3 elements having the same value.
String[] strings = { "the", "quick", "brown", "fox", "jumps",
                     "over", "the", "lazy", "dog", "in", "the",
                     "barn" };
// Display the elements of the array.
Console.WriteLine("The array contains the following values:");
for (int i = strings.GetLowerBound(0); i <= strings.GetUpperBound(0); i++)
   Console.WriteLine("   [{0,2}]: {1}", i, strings[i]);
// Search for the first occurrence of the duplicated value.
string searchString = "the";
int index = Array.IndexOf(strings, searchString);
Console.WriteLine("The first occurrence of \"{0}\" is at index {1}.",
                  searchString, index);
// Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4);
Console.WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
                  searchString, index);
// Search for the first occurrence of the duplicated value in a section of the array.
int position = index + 1;
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1);
Console.WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
                  searchString, position, strings.GetUpperBound(0), index);
// The example displays the following output:
//    The array contains the following values:
//       [ 0]: the
//       [ 1]: quick
//       [ 2]: brown
//       [ 3]: fox
//       [ 4]: jumps
//       [ 5]: over
//       [ 6]: the
//       [ 7]: lazy
//       [ 8]: dog
//       [ 9]: in
//       [10]: the
//       [11]: barn
//    The first occurrence of "the" is at index 0.
//    The first occurrence of "the" between index 4 and the end is at index 6.
//    The first occurrence of "the" between index 7 and index 11 is at index 10.
Public Module Example
   Public Sub Main()
      ' Create a string array with 3 elements having the same value.
      Dim strings() As String = { "the", "quick", "brown", "fox",
                                  "jumps", "over", "the", "lazy",
                                  "dog", "in", "the", "barn" }
      ' Display the values of the array.
      Console.WriteLine("The array contains the following values:")
      For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
         Console.WriteLine("   [{0,2}]: {1}", i, strings(i))
      ' Search for the first occurrence of the duplicated value.
      Dim searchString As String = "the"
      Dim index As Integer = Array.IndexOf(strings, searchString)
      Console.WriteLine("The first occurrence of ""{0}"" is at index {1}.",
                        searchString, index)
      ' Search for the first occurrence of the duplicated value in the last section of the array.
      index = Array.IndexOf(strings, searchString, 4)
      Console.WriteLine("The first occurrence of ""{0}"" between index 4 and the end is at index {1}.",
                        searchString, index)
      ' Search for the first occurrence of the duplicated value in a section of the array.
       Dim position As Integer = index + 1
       index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1)
       Console.WriteLine("The first occurrence of ""{0}"" between index {1} and index {2} is at index {3}.",
                         searchString, position, strings.GetUpperBound(0), index)
    End Sub
End Module
' The example displays the following output:
'    The array contains the following values:
'       [ 0]: the
'       [ 1]: quick
'       [ 2]: brown
'       [ 3]: fox
'       [ 4]: jumps
'       [ 5]: over
'       [ 6]: the
'       [ 7]: lazy
'       [ 8]: dog
'       [ 9]: in
'       [10]: the
'       [11]: barn
'    The first occurrence of "the" is at index 0.
'    The first occurrence of "the" between index 4 and the end is at index 6.
'    The first occurrence of "the" between index 7 and index 11 is at index 10.
    	

這個方法會從索引 startIndex 處的 元素到最後一個專案搜尋一維陣列。 若要判斷 value 是否存在於 中 array ,方法會呼叫 Equals 每個元素的 方法,直到找到相符專案為止,執行相等比較。 這表示,如果專案覆寫 Object.Equals(Object) 方法,則會呼叫該覆寫。

因為大部分陣列的下限為零,所以如果 value 找不到,這個方法通常會傳回 -1。 在罕見的情況下,陣列的下限等於 Int32.MinValue (0x80000000) 且 value 找不到,此方法會 Int32.MaxValue 傳回 (0x7FFFFFFF) 。

如果 startIndex 等於 Array.Length ,方法會傳回 -1。 如果 startIndex 大於 Array.Length ,方法會 ArgumentOutOfRangeException 擲回 。

這個方法是 O (n) 作業,其中 n 是從 到 結尾的專案 array 數目 startIndex

public:
 static int IndexOf(Array ^ array, System::Object ^ value, int startIndex, int count);
public static int IndexOf (Array array, object value, int startIndex, int count);
public static int IndexOf (Array array, object? value, int startIndex, int count);
static member IndexOf : Array * obj * int * int -> int
Public Shared Function IndexOf (array As Array, value As Object, startIndex As Integer, count As Integer) As Integer
IndexOf(Array, Object, Int32, Int32),從最後一次成功比對到陣列結尾的專案,判斷字串陣列中第一個出現的字串 「the」。 若要判斷引數的值 count ,它會從起始索引減去陣列的上限,並加入一個。

using namespace System;
void main()
   // Create a string array with 3 elements having the same value.
   array<String^>^ strings = { "the", "quick", "brown", "fox",
                               "jumps", "over", "the", "lazy", "dog",
                               "in", "the", "barn" };
   // Display the elements of the array.
   Console::WriteLine("The array contains the following values:");
   for (int i = strings->GetLowerBound(0); i <= strings->GetUpperBound(0); i++)
      Console::WriteLine("   [{0,2}]: {1}", i, strings[i]);
   // Search for the first occurrence of the duplicated value.
   String^ searchString =  "the";
   int index = Array::IndexOf(strings, searchString);
   Console::WriteLine("The first occurrence of \"{0}\" is at index {1}.",
                      searchString, index);
   // Search for the first occurrence of the duplicated value in the last section of the array.
   index = Array::IndexOf( strings, searchString, 4);
   Console::WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
                      searchString, index);
   // Search for the first occurrence of the duplicated value in a section of the array.
   int position = index + 1;
   index = Array::IndexOf(strings, searchString, position, strings->GetUpperBound(0) - position + 1);
   Console::WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
                      searchString, position, strings->GetUpperBound(0), index);
// The example displays the following output:
//    The array contains the following values:
//       [ 0]: the
//       [ 1]: quick
//       [ 2]: brown
//       [ 3]: fox
//       [ 4]: jumps
//       [ 5]: over
//       [ 6]: the
//       [ 7]: lazy
//       [ 8]: dog
//       [ 9]: in
//       [10]: the
//       [11]: barn
//    The first occurrence of "the" is at index 0.
//    The first occurrence of "the" between index 4 and the end is at index 6.
//    The first occurrence of "the" between index 7 and index 11 is at index 10.
// Create a string array with 3 elements having the same value.
let strings = 
    [| "the"; "quick"; "brown"; "fox"; "jumps"; "over"
       "the"; "lazy"; "dog"; "in"; "the"; "barn" |]
// Display the elements of the array.
printfn "The array contains the following values:"
for i = strings.GetLowerBound 0 to strings.GetUpperBound 0 do
    printfn $"   [{i,2}]: {strings[i]}"
// Search for the first occurrence of the duplicated value.
let searchString = "the"
let index = Array.IndexOf(strings, searchString)
printfn $"The first occurrence of \"{searchString}\" is at index {index}."
// Search for the first occurrence of the duplicated value in the last section of the array.
let index = Array.IndexOf(strings, searchString, 4)
printfn $"The first occurrence of \"{searchString}\" between index 4 and the end is at index {index}."
// Search for the first occurrence of the duplicated value in a section of the array.
let position = index + 1
let index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound 0 - position + 1)
printfn $"The first occurrence of \"{searchString}\" between index {position} and index {strings.GetUpperBound 0} is at index {index}."
// The example displays the following output:
//    The array contains the following values:
//       [ 0]: the
//       [ 1]: quick
//       [ 2]: brown
//       [ 3]: fox
//       [ 4]: jumps
//       [ 5]: over
//       [ 6]: the
//       [ 7]: lazy
//       [ 8]: dog
//       [ 9]: in
//       [10]: the
//       [11]: barn
//    The first occurrence of "the" is at index 0.
//    The first occurrence of "the" between index 4 and the end is at index 6.
//    The first occurrence of "the" between index 7 and index 11 is at index 10.
// Create a string array with 3 elements having the same value.
String[] strings = { "the", "quick", "brown", "fox", "jumps",
                     "over", "the", "lazy", "dog", "in", "the",
                     "barn" };
// Display the elements of the array.
Console.WriteLine("The array contains the following values:");
for (int i = strings.GetLowerBound(0); i <= strings.GetUpperBound(0); i++)
   Console.WriteLine("   [{0,2}]: {1}", i, strings[i]);
// Search for the first occurrence of the duplicated value.
string searchString = "the";
int index = Array.IndexOf(strings, searchString);
Console.WriteLine("The first occurrence of \"{0}\" is at index {1}.",
                  searchString, index);
// Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4);
Console.WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
                  searchString, index);
// Search for the first occurrence of the duplicated value in a section of the array.
int position = index + 1;
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1);
Console.WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
                  searchString, position, strings.GetUpperBound(0), index);
// The example displays the following output:
//    The array contains the following values:
//       [ 0]: the
//       [ 1]: quick
//       [ 2]: brown
//       [ 3]: fox
//       [ 4]: jumps
//       [ 5]: over
//       [ 6]: the
//       [ 7]: lazy
//       [ 8]: dog
//       [ 9]: in
//       [10]: the
//       [11]: barn
//    The first occurrence of "the" is at index 0.
//    The first occurrence of "the" between index 4 and the end is at index 6.
//    The first occurrence of "the" between index 7 and index 11 is at index 10.
Public Module Example
   Public Sub Main()
      ' Create a string array with 3 elements having the same value.
      Dim strings() As String = { "the", "quick", "brown", "fox",
                                  "jumps", "over", "the", "lazy",
                                  "dog", "in", "the", "barn" }
      ' Display the values of the array.
      Console.WriteLine("The array contains the following values:")
      For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
         Console.WriteLine("   [{0,2}]: {1}", i, strings(i))
      ' Search for the first occurrence of the duplicated value.
      Dim searchString As String = "the"
      Dim index As Integer = Array.IndexOf(strings, searchString)
      Console.WriteLine("The first occurrence of ""{0}"" is at index {1}.",
                        searchString, index)
      ' Search for the first occurrence of the duplicated value in the last section of the array.
      index = Array.IndexOf(strings, searchString, 4)
      Console.WriteLine("The first occurrence of ""{0}"" between index 4 and the end is at index {1}.",
                        searchString, index)
      ' Search for the first occurrence of the duplicated value in a section of the array.
       Dim position As Integer = index + 1
       index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1)
       Console.WriteLine("The first occurrence of ""{0}"" between index {1} and index {2} is at index {3}.",
                         searchString, position, strings.GetUpperBound(0), index)
    End Sub
End Module
' The example displays the following output:
'    The array contains the following values:
'       [ 0]: the
'       [ 1]: quick
'       [ 2]: brown
'       [ 3]: fox
'       [ 4]: jumps
'       [ 5]: over
'       [ 6]: the
'       [ 7]: lazy
'       [ 8]: dog
'       [ 9]: in
'       [10]: the
'       [11]: barn
'    The first occurrence of "the" is at index 0.
'    The first occurrence of "the" between index 4 and the end is at index 6.
'    The first occurrence of "the" between index 7 and index 11 is at index 10.
    	

如果 count 大於 0,這個方法會將一維陣列的專案從 startIndex 搜尋為 startIndexcount 減 1。 若要判斷 value 是否存在於 中 array ,方法會呼叫 Equals 每個元素的 方法,直到找到相符專案為止,執行相等比較。 這表示,如果專案覆寫 Object.Equals 方法,則會呼叫該覆寫。

因為大部分陣列的下限為零,所以找不到時 value ,這個方法通常會傳回 -1。 在罕見的情況下,陣列的下限等於 Int32.MinValue (0x80000000) 且 value 找不到,此方法會 Int32.MaxValue 傳回 (0x7FFFFFFF) 。

如果 startindex 等於 Array.Length ,則方法會傳回 -1。 如果 startIndex 大於 Array.Length ,方法會 ArgumentOutOfRangeException 擲回 。

這個方法是 O (n) 作業,其中 ncount

public:
generic <typename T>
 static int IndexOf(cli::array <T> ^ array, T value, int startIndex);
public static int IndexOf<T> (T[] array, T value, int startIndex);
static member IndexOf : 'T[] * 'T * int -> int
Public Shared Function IndexOf(Of T) (array As T(), value As T, startIndex As Integer) As Integer

下列範例示範 方法的所有三個泛型多載 IndexOf 。 系統會建立字串陣列,其中一個專案會出現兩次,位於索引位置 0 和索引位置 5。 方法 IndexOf<T>(T[], T) 多載會從開頭搜尋陣列,並尋找第一個出現的字串。 方法 IndexOf<T>(T[], T, Int32) 多載是用來搜尋開頭為索引位置 3 的陣列,並繼續到陣列結尾,並尋找第二個出現的字串。 最後, IndexOf<T>(T[], T, Int32, Int32) 方法多載是用來搜尋兩個專案的範圍,從索引位置 2 開始;它會傳回 -1,因為該範圍中沒有搜尋字串的實例。

using namespace System;
void main()
    array<String^>^ dinosaurs = { "Tyrannosaurus", 
        "Amargasaurus",
        "Mamenchisaurus",
        "Brachiosaurus",
        "Deinonychus",
        "Tyrannosaurus",
        "Compsognathus" };
    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs )
        Console::WriteLine(dinosaur);
    Console::WriteLine("\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): {0}", 
        Array::IndexOf(dinosaurs, "Tyrannosaurus"));
    Console::WriteLine("\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}", 
        Array::IndexOf(dinosaurs, "Tyrannosaurus", 3));
    Console::WriteLine("\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): {0}", 
        Array::IndexOf(dinosaurs, "Tyrannosaurus", 2, 2));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
string[] dinosaurs = { "Tyrannosaurus",
    "Amargasaurus",
    "Mamenchisaurus",
    "Brachiosaurus",
    "Deinonychus",
    "Tyrannosaurus",
    "Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
    Console.WriteLine(dinosaur);
Console.WriteLine(
    "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
    Array.IndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
    "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
    Array.IndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
    "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): {0}",
    Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
open System
let dinosaurs =
    [| "Tyrannosaurus"
       "Amargasaurus"
       "Mamenchisaurus"
       "Brachiosaurus"
       "Deinonychus"
       "Tyrannosaurus"
       "Compsognathus" |]
printfn ""
for dino in dinosaurs do
    printfn $"{dino}"
Array.IndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): %i"
// This code example produces the following output:
//    Tyrannosaurus
//    Amargasaurus
//    Mamenchisaurus
//    Brachiosaurus
//    Deinonychus
//    Tyrannosaurus
//    Compsognathus
//    Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
//    Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
//    Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
Public Class Example
    Public Shared Sub Main()
        Dim dinosaurs() As String = { "Tyrannosaurus", _
            "Amargasaurus", _
            "Mamenchisaurus", _
            "Brachiosaurus", _
            "Deinonychus", _
            "Tyrannosaurus", _
            "Compsognathus" }
        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus"))
        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus", 3))
        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 2, 2): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2))
    End Sub
End Class
' This code example produces the following output:
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
    	

這個方法會從 位於 startIndex 的 元素搜尋到陣列結尾的一維陣列。 若要判斷 value 是否存在於 array 中,方法會在每個元素上呼叫 T.Equals 方法來執行相等比較。 這表示如果 T 覆寫 Equals 方法,則會呼叫該覆寫。

如果 startIndex 等於 Length ,則方法會傳回 -1。 如果 startIndex 大於 Array.Length ,則方法會擲回 ArgumentOutOfRangeException

這個方法是 O (n) 作業,其中 n 是 從 startIndex 到 結尾的專案 array 數目。

public:
generic <typename T>
 static int IndexOf(cli::array <T> ^ array, T value, int startIndex, int count);
public static int IndexOf<T> (T[] array, T value, int startIndex, int count);
static member IndexOf : 'T[] * 'T * int * int -> int
Public Shared Function IndexOf(Of T) (array As T(), value As T, startIndex As Integer, count As Integer) As Integer

下列範例示範 方法的所有三個泛型多載 IndexOf 。 系統會建立字串陣列,其中一個專案會出現兩次,位於索引位置 0 和索引位置 5。 方法 IndexOf<T>(T[], T) 多載會從開頭搜尋陣列,並尋找第一個出現的字串。 方法 IndexOf<T>(T[], T, Int32) 多載是用來搜尋開頭為索引位置 3 的陣列,並繼續進行陣列結尾,並尋找字串的第二個出現專案。 最後, IndexOf<T>(T[], T, Int32, Int32) 方法多載是用來搜尋兩個專案的範圍,從索引位置 2 開始;它會傳回 -1,因為該範圍中沒有搜尋字串的實例。

using namespace System;
void main()
    array<String^>^ dinosaurs = { "Tyrannosaurus", 
        "Amargasaurus",
        "Mamenchisaurus",
        "Brachiosaurus",
        "Deinonychus",
        "Tyrannosaurus",
        "Compsognathus" };
    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs )
        Console::WriteLine(dinosaur);
    Console::WriteLine("\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): {0}", 
        Array::IndexOf(dinosaurs, "Tyrannosaurus"));
    Console::WriteLine("\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}", 
        Array::IndexOf(dinosaurs, "Tyrannosaurus", 3));
    Console::WriteLine("\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): {0}", 
        Array::IndexOf(dinosaurs, "Tyrannosaurus", 2, 2));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
string[] dinosaurs = { "Tyrannosaurus",
    "Amargasaurus",
    "Mamenchisaurus",
    "Brachiosaurus",
    "Deinonychus",
    "Tyrannosaurus",
    "Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
    Console.WriteLine(dinosaur);
Console.WriteLine(
    "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
    Array.IndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
    "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
    Array.IndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
    "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): {0}",
    Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
open System
let dinosaurs =
    [| "Tyrannosaurus"
       "Amargasaurus"
       "Mamenchisaurus"
       "Brachiosaurus"
       "Deinonychus"
       "Tyrannosaurus"
       "Compsognathus" |]
printfn ""
for dino in dinosaurs do
    printfn $"{dino}"
Array.IndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): %i"
// This code example produces the following output:
//    Tyrannosaurus
//    Amargasaurus
//    Mamenchisaurus
//    Brachiosaurus
//    Deinonychus
//    Tyrannosaurus
//    Compsognathus
//    Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
//    Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
//    Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
Public Class Example
    Public Shared Sub Main()
        Dim dinosaurs() As String = { "Tyrannosaurus", _
            "Amargasaurus", _
            "Mamenchisaurus", _
            "Brachiosaurus", _
            "Deinonychus", _
            "Tyrannosaurus", _
            "Compsognathus" }
        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus"))
        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus", 3))
        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 2, 2): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2))
    End Sub
End Class
' This code example produces the following output:
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
    	

如果 count 大於 0,這個方法會從 搜尋一維陣列 startIndex 的專案,以 startIndexcount 減 1。 若要判斷 value 是否存在於 array 中,方法會在每個元素上呼叫 T.Equals 方法來執行相等比較。 這表示如果 T 覆寫 Equals 方法,則會呼叫該覆寫。

如果 startIndex 等於 Array.Length ,則方法會傳回 -1。 如果 startIndex 大於 Array.Length ,則方法會擲回 ArgumentOutOfRangeException

這個方法是 O (n) 作業,其中 ncount

generic <typename T> static int IndexOf(cli::array <T> ^ array, T value);
public static int IndexOf<T> (T[] array, T value);
static member IndexOf : 'T[] * 'T -> int
Public Shared Function IndexOf(Of T) (array As T(), value As T) As Integer

下列範例示範 方法的所有三個泛型多載 IndexOf 。 系統會建立字串陣列,其中一個專案會出現兩次,位於索引位置 0 和索引位置 5。 方法 IndexOf<T>(T[], T) 多載會從開頭搜尋陣列,並尋找第一個出現的字串。 方法 IndexOf<T>(T[], T, Int32) 多載是用來搜尋開頭為索引位置 3 的陣列,並繼續進行陣列結尾,並尋找字串的第二個出現專案。 最後, IndexOf<T>(T[], T, Int32, Int32) 方法多載是用來搜尋兩個專案的範圍,從索引位置 2 開始;它會傳回 -1,因為該範圍中沒有搜尋字串的實例。

using namespace System;
void main()
    array<String^>^ dinosaurs = { "Tyrannosaurus", 
        "Amargasaurus",
        "Mamenchisaurus",
        "Brachiosaurus",
        "Deinonychus",
        "Tyrannosaurus",
        "Compsognathus" };
    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs )
        Console::WriteLine(dinosaur);
    Console::WriteLine("\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): {0}", 
        Array::IndexOf(dinosaurs, "Tyrannosaurus"));
    Console::WriteLine("\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}", 
        Array::IndexOf(dinosaurs, "Tyrannosaurus", 3));
    Console::WriteLine("\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): {0}", 
        Array::IndexOf(dinosaurs, "Tyrannosaurus", 2, 2));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
string[] dinosaurs = { "Tyrannosaurus",
    "Amargasaurus",
    "Mamenchisaurus",
    "Brachiosaurus",
    "Deinonychus",
    "Tyrannosaurus",
    "Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
    Console.WriteLine(dinosaur);
Console.WriteLine(
    "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
    Array.IndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
    "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
    Array.IndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
    "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): {0}",
    Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
open System
let dinosaurs =
    [| "Tyrannosaurus"
       "Amargasaurus"
       "Mamenchisaurus"
       "Brachiosaurus"
       "Deinonychus"
       "Tyrannosaurus"
       "Compsognathus" |]
printfn ""
for dino in dinosaurs do
    printfn $"{dino}"
Array.IndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): %i"
// This code example produces the following output:
//    Tyrannosaurus
//    Amargasaurus
//    Mamenchisaurus
//    Brachiosaurus
//    Deinonychus
//    Tyrannosaurus
//    Compsognathus
//    Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
//    Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
//    Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
Public Class Example
    Public Shared Sub Main()
        Dim dinosaurs() As String = { "Tyrannosaurus", _
            "Amargasaurus", _
            "Mamenchisaurus", _
            "Brachiosaurus", _
            "Deinonychus", _
            "Tyrannosaurus", _
            "Compsognathus" }
        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus"))
        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus", 3))
        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 2, 2): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2))
    End Sub
End Class
' This code example produces the following output:
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
    	

這個方法會搜尋一維陣列的所有專案。 value 若要判斷 value 是否存在於 array 中,方法會在每個元素上呼叫 T.Equals 方法來執行相等比較。 這表示如果 T 覆寫 Equals 方法,則會呼叫該覆寫。

這個方法是 O (n) 作業,其中 nLengtharray