Mono Class Library: System.Array Overview | Members

System.Array.IndexOf Method

Searches the specified one-dimensional Array, returning the index of the first occurrence of the specified object.

[System.Runtime.ConstrainedExecution.ReliabilityContract(System.Runtime.ConstrainedExecution.Consistency.WillNotCorruptState, System.Runtime.ConstrainedExecution.Cer.MayFail)]
public static int IndexOf (Array array, object value)

Parameters

array
A one-dimensional Array to search.
value
A object to locate in array.

Returns

A int containing the index of the first occurrence of value in array, if found; otherwise, array.GetLowerBound(0) - 1.
Note: For a vector, if value is not found, the return value will be -1. This provides the caller with a standard code for a failed search.

Exceptions

TypeReason
ArgumentNullExceptionarray is null.
RankExceptionarray has more than one dimension.

Remarks

This version of Array.IndexOf(Array, object) is equivalent to Array.IndexOf(Array, object)(array, value, array.GetLowerBound(0),array.Length).

The elements are compared using object.Equals(object).

Example

The following example demonstrates the Array.IndexOf(Array, object) method.

C# Example

using System;
public class ArrayIndexOfExample {
   public static void Main() {
      int[] intAry = { 0, 1, 2, 0, 1 };
      Console.Write( "The values of the array are: " );
      foreach( int i in intAry )
         Console.Write( "{0,5}", i );
      Console.WriteLine();
      int j = Array.IndexOf( intAry, 1 );
      Console.WriteLine( "The first occurrence of 1 is at index {0}", j );
   }
}

The output is

The values of the array are: 0 1 2 0 1
The first occurrence of 1 is at index 1

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0