Mono Class Library: System.Array Overview | Members

System.Array.LastIndexOf Method

Searches the specified one-dimensional Array, returning the index of the last occurrence of the specified object. [Edit]

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

Parameters

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

Returns

A int containing the index of the last occurrence in array of value, 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 the failed search.
[Edit]

Exceptions

TypeReason
ArgumentNullExceptionarray is null . [Edit]
RankExceptionarray has more than one dimension. [Edit]

Remarks

This version of Array.LastIndexOf(Array, object) is equivalent to Array.LastIndexOf(Array, object)(array, value, (array.GetLowerBound(0) + array.Length - 1), array.Length).

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

[Edit]

Example

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

C# Example
using System;

public class ArrayLastIndexOfExample {

   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.LastIndexOf( intAry, 1 );
      Console.WriteLine( "The last occurrence of 1 is at index {0}", j );
   }
}

The output is

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

Requirements

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