C# Array LastIndexOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In C#, Array.LastIndexOf searches from the end of the array (or from a starting index backward) and returns the last position that equals your value.

If the value is absent, the result is -1. Use it when duplicates exist and you care about the rightmost match.

This is a static member of System.Array. You call it with the class name, such as Array.LastIndexOf(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.

Syntax

Pass the array and value. Compare the result with -1.

C#
int ix = Array.LastIndexOf(nums, 30);
CallWhat it does
Array.LastIndexOf(nums, 30)Last index of 30, or -1.
Array.LastIndexOf(nums, 30, 3)Search backward starting from index 3.

Pair with IndexOf when you need both the first and last positions.

Library declarations
C#
public static int LastIndexOf(Array array, object value);
public static int LastIndexOf<T>(T[] array, T value);

Parameters

ParameterDescription
arrayRequired. Array to search.
valueRequired. Value to find.
startIndexOptional. Index to start searching backward from.

Return Value

Return ValueDescription
indexLast matching position.
-1Not found.

C# Examples

Example 1 — Find the Last Duplicate

Two 30s exist; LastIndexOf returns the later one.

C#
using System;

class Program {
    static void Main() {
        int[] nums = { 30, 10, 30, 20 };
        int ix = Array.LastIndexOf(nums, 30);

        Console.WriteLine("Last 30 at " + ix);
        Console.WriteLine("First 30 at " + Array.IndexOf(nums, 30));
    }
}

How It Works

  1. Rightmost win. Scanning from the end finds index 2 first.
  2. IndexOf contrast. The first match is still at 0.
  3. Duplicates. LastIndexOf shines when values repeat.
  4. Unsorted OK. No Sort call is required.

Example 2 — Return -1 When Absent

Search for a string that is not present.

C#
using System;

class Program {
    static void Main() {
        string[] colors = { "red", "blue" };
        int ix = Array.LastIndexOf(colors, "green");
        Console.WriteLine(ix);
    }
}

How It Works

  1. Same sentinel. Missing values use -1, just like IndexOf.
  2. Check before use. Never index with -1.
  3. Short array. Both ends were checked; nothing matched.
  4. Case rules. Default string compare is case-sensitive.

Example 3 — Limit the Backward Search

Start from a middle index so earlier matches are ignored.

C#
using System;

class Program {
    static void Main() {
        int[] nums = { 7, 3, 7, 3, 7 };
        // start at index 3, search toward 0
        int ix = Array.LastIndexOf(nums, 7, 3);

        Console.WriteLine(ix);
    }
}

How It Works

  1. Start at 3. Position 3 holds 3, so the search continues left.
  2. Finds index 2. That is the last 7 at or before index 3.
  3. Skips final 7. The 7 at index 4 is outside the search window.
  4. Useful for ranges. Combine with count overloads for tighter windows.

Frequently Asked Questions

It returns -1.
No. It works on any order.
When you want the rightmost match among duplicates.
Yes. Overloads accept a startIndex and optional count for a backward window.
Call IndexOf and LastIndexOf with the same value.

Next: Resize()

Replace an array with a new one of a different length (ref parameter) with System.Array.

Resize() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful