
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# Array - LastIndexOf() Method
The C# Array LastIndexOf() method is used to search for the specified element or object in an array and return the index of its last occurrence. This method is manly useful when we want to find the last position of a value in a one-dimensional array.
The one-dimensional Array is searched backward starting at the last element and ending at the first element.
Exceptions
There are following exception of this methods −
- ArgumentNullException: When array is null.
- RankException: When array is multidimensional.
Syntax
Following are the syntax of the C# Array LastIndexOf() method −
public static int LastIndexOf (Array array, object? value);
This syntax of
Array.LastIndexOf(Array, Object, Int32)
This syntax of
Array.LastIndexOf(Array, Object, Int32, Int32)
Parameters
This method accepts the following parameters −
- array: It specifies the one-dimensional array to search.
- object: The object to locate in array.
Return value
This method returns index of the last occurrence of value in array, if found; otherwise, the lower bound of the array -1.
Example 1: Last Index of the Specified String
This is the basic example of the LastIndexOf() method to display the last index of the specified element −
using System; class Program { static void Main() { string[] fruits = { "Apple", "Banana", "Cherry", "Banana", "Grapes" }; int index = Array.LastIndexOf(fruits, "Banana"); Console.WriteLine($"Last occurrence of 'Banana': {index}"); } }
Output
Following is the output −
Last occurrence of 'Banana': 3
Example 2: Search Last Index From Specific Index
Let's create another example of the LastIndexOf() method to display the last index of the specified element starts from the last given position −
using System; class Program { static void Main() { int[] numbers = { 1, 2, 3, 4, 3, 5, 6 }; int index = Array.LastIndexOf(numbers, 3, 3); Console.WriteLine($"Index of '3' starting from last position 3: {index}"); } }
Output
Following is the output −
Index of '3' starting from last position 3: 2
Example 3: Search Last Index in a Range
Let's see another example of the LastIndexOf() method, Here we specified the range to search a last index of an element within this range −
using System; class Program { static void Main() { char[] letters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' }; int index = Array.LastIndexOf(letters, 'f', 7, 4); Console.WriteLine($"Last Index of 'f': {index}"); } }
Output
Following is the output −
Last Index of 'f': 5
Example 4: Search for a Missing Element
Let's see another example, the LastIndexOf() method. The LastIndexOf display -1 if the element or object is not present in the array −
using System; class Program { static void Main() { double[] values = { 1.1, 2.2, 3.3, 4.4, 5.5 }; int index = Array.LastIndexOf(values, 6.6); Console.WriteLine($"Last Index of 6.6: {index}"); } }
Output
Following is the output −
Last Index of 6.6: -1