LastIndexOf ExamplesΒΆ

The general syntax for this method is:

Array.IndexOf(arrayName, item)

This method returns the index of the LAST occurence of an item in the array. If the item is not in the array, -1 is returned.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
string[] pets = new string[]
{
   "Alyce",
   "Thisbe",
   "Francis",
   "Scratch",
   "Beatrice",
   "Shadow",
   "Beatrice"
};

Console.WriteLine(Array.LastIndexOf(pets, "Beatrice"));
Console.WriteLine(Array.LastIndexOf(pets, "Poppy"));

Output

6
-1