Method IndexOf
| Edit this pageIndexOf<TSource>(IEnumerable<TSource>, TSource)
Searches for the specified object and returns the zero-based index of the first occurrence within the entire IEnumerable<T>.
Declaration
public static int IndexOf<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 first 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.
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 an element in a sequence using IndexOf
.
var sequence = new[]
{
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
}.AsEnumerable();
// Find the element `3` in the sequence
var result = sequence
.IndexOf(3);
Console.WriteLine($"Index: {result}");
// This code produces the following output:
// Index: 2
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
IndexOf<TSource>(IEnumerable<TSource>, TSource, Index)
Searches for the specified object 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 IndexOf<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 starting element within the sequence. |
Returns
Type | Description |
---|---|
int | The zero-based index of the first occurrence of |
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.
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 an element in a sequence using IndexOf
.
var sequence = new[]
{
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
}.AsEnumerable();
// Find the element `3` in the sequence in the range [5..]
var result = sequence
.IndexOf(3, 5);
Console.WriteLine($"Index: {result}");
// This code produces the following output:
// Index: 7
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
IndexOf<TSource>(IEnumerable<TSource>, TSource, Index, int)
Searches for the specified object 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 IndexOf<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 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 |
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
.
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 an element in a sequence using IndexOf
.
var sequence = new[]
{
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
}.AsEnumerable();
// Find the element `3` in the sequence in the range [5..7]
var result = sequence
.IndexOf(3, 5, 2);
Console.WriteLine($"Index: {result}");
// This code produces the following output:
// Index: -1
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|