C# Array LastIndexOf(Array, Object, Int32, Int32)

Description

Array LastIndexOf(Array, Object, Int32, 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 contains the specified number of elements and ends at the specified index.

Syntax

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


public static int LastIndexOf(
  Array array,//w w  w .  j  ava2s. com
  Object value,
  int startIndex,
  int count
)

Parameters

Array.LastIndexOf(Array, Object, Int32, 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.
  • count - The number of elements in the section to search.

Returns

Array.LastIndexOf(Array, Object, Int32, Int32) method returns The index of the last occurrence of value within the range of elements in array that contains the number of elements specified in count and ends at 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.


//from   ww  w .  ja  v a2  s. com
using System;
public class SamplesArray  {

   public static void Main()  {
      Array myArray=Array.CreateInstance( typeof(String), 12 );
      myArray.SetValue( "S", 0 );
      myArray.SetValue( "X", 1 );
      myArray.SetValue( "E", 2 );
      myArray.SetValue( "T", 3 );
      myArray.SetValue( "H", 4 );
      myArray.SetValue( "W", 5 );
      myArray.SetValue( "Q", 6 );
      myArray.SetValue( "B", 7 );
      myArray.SetValue( "Z", 8 );
      myArray.SetValue( "U", 9 );
      myArray.SetValue( "P", 10 );
      myArray.SetValue( "I", 11 );

      int myIndex = Array.LastIndexOf( myArray, "Q", 10, 6 );
      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