C# Array LastIndexOf(Array, Object, Int32)

Description

Array LastIndexOf(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.

Syntax

Array.LastIndexOf(Array, Object, Int32) has the following syntax.


public static int LastIndexOf(
  Array array,/*  w w w .ja v a 2 s .  c o  m*/
  Object value,
  int startIndex
)

Parameters

Array.LastIndexOf(Array, Object, 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 backward search.

Returns

Array.LastIndexOf(Array, Object, Int32) method returns 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.

Example

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


using System;/*w w w .  ja  v  a  2 s . c  o  m*/
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.LastIndexOf( myArray, "the", 8 );
      Console.WriteLine(myIndex );

   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version