Method FindIndex
| Edit this page View SourceFindIndex<TSource>(IEnumerable<TSource>, Func<TSource, bool>)
Searches for an element that matches the conditions defined by the specified predicate and returns the zero-based index of the first occurrence within the entire IEnumerable<T>.
Declaration
public static int FindIndex<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
Func<TSource, bool> | predicate | The predicate that defines the conditions of the element to search for. |
Returns
Type | Description |
---|---|
int | The zero-based index of the first occurrence of an element that matches the conditions defined by |
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.
The predicate
is a delegate to a method that returns true if the object
passed to it matches the conditions defined in the delegate. The elements of the current IEnumerable<T> are individually passed to the predicate
delegate.
This operator executes immediately.
Examples
The following code example demonstrates how to find the index of the first element that satisfies a condition, using FindIndex
.
var sequence = new[]
{
(key: 2, name: "Frank"),
(key: 3, name: "Jill"),
(key: 5, name: "Dave"),
(key: 8, name: "Jack"),
(key: 12, name: "Judith"),
(key: 14, name: "Robert"),
(key: 1, name: "Adam"),
};
// Find the first index that matches a condition
Console.WriteLine(
"'J' starts at index {0}",
sequence.FindIndex(x => x.name.StartsWith("J")));
Console.WriteLine(
"'Ju' starts at index {0}",
sequence.FindIndex(x => x.name.StartsWith("Ju")));
Console.WriteLine(
"'K' starts at index {0}",
sequence.FindIndex(x => x.name.StartsWith("K")));
// This code produces the following output:
// 'J' starts at index 1
// 'Ju' starts at index 4
// 'K' starts at index -1
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
FindIndex<TSource>(IEnumerable<TSource>, Func<TSource, bool>, Index)
Searches for an element that matches the conditions defined by the specified predicate and returns the zero-based index of the first occurrence within the range of elements in the IEnumerable<T> that extends from the specified index to the last element.
Declaration
public static int FindIndex<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Index index)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
Func<TSource, bool> | predicate | The predicate that defines the conditions of the element to search for. |
Index | index | The Index of the starting element within the sequence. |
Returns
Type | Description |
---|---|
int | The zero-based index of the first occurrence of an element that matches the conditions defined by |
Type Parameters
Name | Description |
---|---|
TSource | The type of elements of |
Remarks
The IEnumerable<T> is searched forward starting at index
and ending at the
last element.
The predicate
is a delegate to a method that returns true if the object
passed to it matches the conditions defined in the delegate. The elements of the current IEnumerable<T> are individually passed to the predicate
delegate.
This operator executes immediately.
Examples
The following code example demonstrates how to find the index of the first element that satisfies a condition, using FindIndex
.
var sequence = new[]
{
(key: 2, name: "Frank"),
(key: 3, name: "Jill"),
(key: 5, name: "Dave"),
(key: 8, name: "Jack"),
(key: 12, name: "Judith"),
(key: 14, name: "Robert"),
(key: 1, name: "Adam"),
};
// Find the first index that matches a condition
Console.WriteLine(
"'J' starts at index {0}, after index 4",
sequence.FindIndex(
x => x.name.StartsWith("J"),
4));
Console.WriteLine(
"'J' starts at index {0}, after index ^2",
sequence.FindIndex(
x => x.name.StartsWith("J"),
^2));
// This code produces the following output:
// 'J' starts at index 4, after index 4
// 'J' starts at index -1, after index ^2
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
FindIndex<TSource>(IEnumerable<TSource>, Func<TSource, bool>, Index, int)
Searches for an element that matches the conditions defined by the specified predicate and returns the zero-based index of the first occurrence within the range of elements in the IEnumerable<T> that starts at the specified index to the last element and contains the specified number of elements.
Declaration
public static int FindIndex<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Index index, int count)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
Func<TSource, bool> | predicate | The predicate that defines the conditions of the element to search for. |
Index | index | The Index of the starting 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 first occurrence of an element that matches the conditions defined by |
Type Parameters
Name | Description |
---|---|
TSource | The type of elements of |
Remarks
The IEnumerable<T> is searched forward starting at index
and ending at
index
plus count
minus 1
, if count is greater than 0
.
The predicate
is a delegate to a method that returns true if the object
passed to it matches the conditions defined in the delegate. The elements of the current IEnumerable<T> are individually passed to the predicate
delegate.
This operator executes immediately.
Examples
The following code example demonstrates how to find the index of the first element that satisfies a condition, using FindIndex
.
var sequence = new[]
{
(key: 2, name: "Frank"),
(key: 3, name: "Jill"),
(key: 5, name: "Dave"),
(key: 8, name: "Jack"),
(key: 12, name: "Judith"),
(key: 14, name: "Robert"),
(key: 1, name: "Adam"),
};
// Find the first index that matches a condition
Console.WriteLine(
"'J' starts at index {0}, after index 2-4",
sequence.FindIndex(
x => x.name.StartsWith("J"),
2,
3));
Console.WriteLine(
"'J' starts at index {0}, after index 2-2",
sequence.FindIndex(
x => x.name.StartsWith("J"),
2,
1));
// This code produces the following output:
// 'J' starts at index 3, after index 2-4
// 'J' starts at index -1, after index 2-2
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|