C# Array LastIndexOf(Array, Object)
Description
Array LastIndexOf(Array, Object)
searches for the specified
object and returns the index of the last occurrence within the entire one-dimensional
Array.
Syntax
Array.LastIndexOf(Array, Object)
has the following syntax.
public static int LastIndexOf(
Array array,
Object value
)
Parameters
Array.LastIndexOf(Array, Object)
has the following parameters.
array
- The one-dimensional Array to search.value
- The object to locate in array.
Returns
Array.LastIndexOf(Array, Object)
method returns The index of the last occurrence of value within the entire array, 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.
/* www . java 2s. co m*/
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 );
String myString = "the";
int myIndex = Array.LastIndexOf( myArray, myString );
Console.WriteLine(myIndex );
}
}
The code above generates the following result.