C# Array IndexOf(Array, Object)
Description
Array IndexOf(Array, Object)
searches for the specified
object and returns the index of the first occurrence within the entire one-dimensional
Array.
Syntax
Array.IndexOf(Array, Object)
has the following syntax.
public static int IndexOf(
Array array,
Object value
)
Parameters
Array.IndexOf(Array, Object)
has the following parameters.
array
- The one-dimensional Array to search.value
- The object to locate in array.
Returns
Array.IndexOf(Array, Object)
method returns The index of the first occurrence of value within the entire array, 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;//ww w.j a v a2 s .com
public class SamplesArray {
public static void Main() {
Array myArray=Array.CreateInstance( typeof(String), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
String myString = "the";
int myIndex = Array.IndexOf( myArray, myString );
Console.WriteLine( "The first occurrence of \"{0}\" is at index {1}.", myString, myIndex );
}
}
The code above generates the following result.