.Net framework ArrayList.LastIndexOf(Object) method allow us to search the specified Object and return the zero-based index of the last occurrence within the entire ArrayList. the arraylist LastIndexOf() method exists in System.Collections namespace.
this method require to pass a parameter named ‘value’. the ‘value’ parameter value type is System.Object which represents the Object to locate in the arraylist. this value can be null.
the LastIndexOf() method return value data type is System.Int32. this return integer value is the zero-based index of the last occurrence of the ‘value’ within the entire arraylist if the object found in arraylist; otherwise the method return -1. if the arraylist contains same object multiple times then the method return last occurrence index of the specified object.
the following console c# example code demonstrate us how can we get the last occurrence index of a specified object within an arraylist programmatically at run time in an console application.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Program { static void Main(string[] args) { ArrayList colors = new ArrayList() { "BlanchedAlmond", "Brown", "Cornsilk", "Brown", "CadetBlue" }; Console.WriteLine("ArrayList Elements...."); foreach (string color in colors) { Console.WriteLine(color); } Console.WriteLine("\nLastIndexOf(object) Method"); Console.WriteLine("Last Index Of 'Brown': " + colors.LastIndexOf("Brown").ToString()); Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
ArrayList Elements.... BlanchedAlmond Brown Cornsilk Brown CadetBlue LastIndexOf(object) Method Last Index Of 'Brown': 3 |