C# Array IndexOf(Array, Object, Int32, Int32)
Description
Array IndexOf(Array, Object, Int32, Int32)
searches
for the specified object and returns the index of the first occurrence within
the range of elements in the one-dimensional Array that starts at the specified
index and contains the specified number of elements.
Syntax
Array.IndexOf(Array, Object, Int32, Int32)
has the following syntax.
public static int IndexOf(
Array array,/*from w ww .j a v a 2s . c o m*/
Object value,
int startIndex,
int count
)
Parameters
Array.IndexOf(Array, Object, Int32, Int32)
has the following parameters.
array
- The one-dimensional Array to search.value
- The object to locate in array.startIndex
- The 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(Array, Object, Int32, Int32)
method returns
The 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, the lower bound of the array minus 1.
Example
The following code example shows how to determine the index of the first occurrence of a specified element.
using System;/*from w w w. j a v a 2 s. c om*/
public class SamplesArray {
public static void Main() {
Array myArray=Array.CreateInstance( typeof(String), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );
int myIndex = Array.IndexOf( myArray, "in", 6, 5 );
Console.WriteLine(myIndex );
}
}
The code above generates the following result.