Search for object and returns the index of the last occurrence within the entire one-dimensional Array in CSharp
Description
The following code shows how to search for object and returns the index of the last occurrence within the entire one-dimensional Array.
Example
//ww w. j a va2 s . c o m
using System;
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 );
String myString = "the";
int myIndex = Array.LastIndexOf( myArray, myString );
Console.WriteLine(myIndex );
}
}
The code above generates the following result.