Export (0) Print
Expand All

Array.LastIndexOf Method (Array, Object, Int32)

Searches for the specified object and returns the index of the last occurrence within the range of elements in the one-dimensional Array that extends from the first element to the specified index.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public static int LastIndexOf (
	Array array,
	Object value,
	int startIndex
)
public static int LastIndexOf (
	Array array, 
	Object value, 
	int startIndex
)
public static function LastIndexOf (
	array : Array, 
	value : Object, 
	startIndex : int
) : int

Parameters

array

The one-dimensional Array to search.

value

The object to locate in array.

startIndex

The starting index of the backward search.

Return Value

The index of the last occurrence of value within the range of elements in array that extends from the first element to startIndex, if found; otherwise, the lower bound of the array minus 1.

Exception typeCondition

ArgumentNullException

array is a null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

startIndex is outside the range of valid indexes for array.

RankException

array is multidimensional.

The one-dimensional Array is searched backward starting at startIndex and ending at the first element.

The elements are compared to the specified value using the Object.Equals method. If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Since most arrays will have a lower bound of zero, this method would generally return –1 when value is not found. In the rare case that the lower bound of the array is equal to Int32.MinValue and value is not found, this method returns Int32.MaxValue, which is System.Int32.MinValue - 1.

This method is an O(n) operation, where n is the number of elements from the beginning of array to startIndex.

The following code example shows how to determine the index of the last occurrence of a specified element in an array.

using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a new Array with three elements of the same value.
      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 );

      // Displays the values of the Array.
      Console.WriteLine( "The Array contains the following values:" );
      PrintIndexAndValues( myArray );

      // Searches for the last occurrence of the duplicated value.
      String myString = "the";
      int myIndex = Array.LastIndexOf( myArray, myString );
      Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

      // Searches for the last occurrence of the duplicated value in the first section of the Array.
      myIndex = Array.LastIndexOf( myArray, myString, 8 );
      Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

      // Searches for the last occurrence of the duplicated value in a section of the Array.
      // Note that the start index is greater than the end index because the search is done backward.
      myIndex = Array.LastIndexOf( myArray, myString, 10, 6 );
      Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
   }


   public static void PrintIndexAndValues( Array myArray )  {
      for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
         Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
   }
}
/* 
This code produces the following output.

The Array contains the following values:
    [0]:    the
    [1]:    quick
    [2]:    brown
    [3]:    fox
    [4]:    jumps
    [5]:    over
    [6]:    the
    [7]:    lazy
    [8]:    dog
    [9]:    in
    [10]:    the
    [11]:    barn
The last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/

import System.*;

public class SamplesArray
{
    public static void main(String[] args)
    {
        // Creates and initializes a new Array with three elements of 
        // the same value.
        Array myArray = Array.CreateInstance(String.class.ToType(), 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);
        // Displays the values of the Array.
        Console.WriteLine("The Array contains the following values:");
        PrintIndexAndValues(myArray);
        // Searches for the last occurrence of the duplicated value.
        String myString = "the";
        int myIndex = Array.LastIndexOf(myArray, myString);
        Console.WriteLine("The last occurrence of \"{0}\" is at index {1}.",
            myString,(Int32)myIndex);
        // Searches for the last occurrence of the duplicated value in the first
        // section of the Array.
        myIndex = Array.LastIndexOf(myArray, myString, 8);
        Console.WriteLine("The last occurrence of \"{0}\" between the start"
            + " and index 8 is at index {1}.", myString, (Int32)myIndex);
        // Searches for the last occurrence of the duplicated value in a 
        // section of the Array.
        // Note that the start index is greater than the end index because the 
        // search is done backward.
        myIndex = Array.LastIndexOf(myArray, myString, 10, 6);
        Console.WriteLine("The last occurrence of \"{0}\" between index 5 and"
            + " index 10 is at index {1}.",myString,(Int32)myIndex);
    } //main

    public static void PrintIndexAndValues(Array myArray)
    {
        for (int i = myArray.GetLowerBound(0); i <= myArray.
            GetUpperBound(0); i++) {
            Console.WriteLine("\t[{0}]:\t{1}",(Int32)i,myArray.GetValue(i));
        }
    } //PrintIndexAndValues
} //SamplesArray 

 /* 
 This code produces the following output.
 
 The Array contains the following values:
     [0]:    the
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumps
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
     [9]:    in
     [10]:    the
     [11]:    barn
 The last occurrence of "the" is at index 10.
 The last occurrence of "the" between the start and index 8 is at index 6.
 The last occurrence of "the" between index 5 and index 10 is at index 10.
 */

import System;

// Creates and initializes a new Array with three elements of the same value.
var myArray : System.Array= System.Array.CreateInstance( System.String, 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumped", 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 );

// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintIndexAndValues( myArray );

// Searches for the last occurrence of the duplicated value.
var myString : String = "the";
var myIndex : int = System.Array.LastIndexOf( myArray, myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = System.Array.LastIndexOf( myArray, myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in a section of the Array. 
// Note that the start index is greater than the end index because the search is done backward.
myIndex = System.Array.LastIndexOf( myArray, myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
 
 
function PrintIndexAndValues( myArray : System.Array )  {
   for ( var i : int = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
      Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
}
 
 /* 
 This code produces the following output.
 
 The Array contains the following values:
     [0]:    the
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
     [9]:    in
     [10]:    the
     [11]:    barn
 The last occurrence of "the" is at index 10.
 The last occurrence of "the" between the start and index 8 is at index 6.
 The last occurrence of "the" between index 5 and index 10 is at index 10.
 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft