Method LastIndexOf
| Edit this pageLastIndexOf<TSource>(IEnumerable<TSource>, TSource)
Searches for the specified object and returns the zero-based index of the last occurrence within the entire IEnumerable<T>.
Declaration
public static int LastIndexOf<TSource>(this IEnumerable<TSource> source, TSource item)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | The source sequence. |
| TSource | item | The object to locate in the IEnumerable<T>. The value can be null for reference types. |
Returns
| Type | Description |
|---|---|
| int | The zero-based index of the last occurrence of |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of elements of |
Remarks
The IEnumerable<T> is searched forward starting at the first element and ending at the last
element, and the index of the last instance of item is returned.
This method determines equality using the default equality comparer Default for TSource, the type of values in the list.
This operator executes immediately.
Examples
The following code example demonstrates how to find the index of the last instance of an element in a sequence using LastIndexOf.
var sequence = new[]
{
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
}.AsEnumerable();
// Find the element `3` in the sequence
var result = sequence
.LastIndexOf(3);
Console.WriteLine($"Index: {result}");
// This code produces the following output:
// Index: 7
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
LastIndexOf<TSource>(IEnumerable<TSource>, TSource, Index)
Searches for the specified object and returns the zero-based index of the last occurrence within the range of elements in the IEnumerable<T> that extends backwards from the specified index to the first element.
Declaration
public static int LastIndexOf<TSource>(this IEnumerable<TSource> source, TSource item, Index index)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | The source sequence. |
| TSource | item | The object to locate in the IEnumerable<T>. The value can be null for reference types. |
| Index | index | The Index of the ending element within the sequence. |
Returns
| Type | Description |
|---|---|
| int | The zero-based index of the last occurrence of |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of elements of |
Remarks
The IEnumerable<T> is searched forward starting at the first element and ending at index, and the index of the last instance of item is returned.
This method determines equality using the default equality comparer Default for TSource, the type of values in the list.
This operator executes immediately.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
LastIndexOf<TSource>(IEnumerable<TSource>, TSource, Index, int)
Searches for the specified object and returns the zero-based index of the last occurrence within the range of elements in the IEnumerable<T> that ends at the specified index to the last element and contains the specified number of elements.
Declaration
public static int LastIndexOf<TSource>(this IEnumerable<TSource> source, TSource item, Index index, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | The source sequence. |
| TSource | item | The object to locate in the IEnumerable<T>. The value can be null for reference types. |
| Index | index | The Index of the ending element within the sequence. |
| int | count | The number of elements in the section to search. |
Returns
| Type | Description |
|---|---|
| int | The zero-based index of the last occurrence of |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of elements of |
Remarks
The IEnumerable<T> is searched forward starting at the first element and ending at index, and the index of the last instance of item no earlier in the sequence
than count items before index is returned.
This method determines equality using the default equality comparer Default for TSource, the type of values in the list.
This operator executes immediately.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentOutOfRangeException |
|