C# Array IndexOf(T[], T, Int32, Int32)
Description
Array IndexOf(T[], T, Int32, Int32)
searches
for the specified object and returns the index of the first occurrence within
the range of elements in the Array that starts at the specified index and contains
the specified number of elements.
Syntax
Array.IndexOf(T[], T, Int32, Int32)
has the following syntax.
public static int IndexOf<T>(
T[] array,//from w w w . ja va 2 s. com
T value,
int startIndex,
int count
)
Parameters
Array.IndexOf(T[], T, Int32, Int32)
has the following parameters.
T
- The type of the elements of the array.array
- The one-dimensional, zero-based Array to search.value
- The object to locate in array.startIndex
- The zero-based starting index of the search. 0 (zero) is valid in an empty array.count
- The number of elements in the section to search.
Returns
Array.IndexOf(T[], T, Int32, Int32)
method returns The zero-based
index of the first occurrence of value within the range of
elements in array that starts at startIndex and contains the number of elements
specified in count, if found; otherwise, -1.
Example
The following code example demonstrates all three generic overloads of the IndexOf method.
/*from w ww. j ava 2s .c om*/
using System;
public class Example
{
public static void Main()
{
string[] myValues = { "2001",
"1999",
"2000",
"1234",
"2002",
"2001",
"4567" };
Console.WriteLine(Array.IndexOf(myValues, "2001"));
Console.WriteLine(Array.IndexOf(myValues, "2001", 3));
Console.WriteLine(Array.IndexOf(myValues, "2001", 2, 2));
}
}
The code above generates the following result.