Edit

Share via


Array.LastIndexOf Method

Definition

Returns the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array.

Overloads

LastIndexOf(Array, Object)

Searches for the specified object and returns the index of the last occurrence within the entire one-dimensional Array.

LastIndexOf(Array, Object, Int32)

Searches for the specified object and returns the index of the last occurrence within the range of elements in the one-dimensional Array that extends from the first element to the specified index.

LastIndexOf(Array, Object, Int32, Int32)

Searches for the specified object and returns the index of the last occurrence within the range of elements in the one-dimensional Array that contains the specified number of elements and ends at the specified index.

LastIndexOf<T>(T[], T)

Searches for the specified object and returns the index of the last occurrence within the entire Array.

LastIndexOf<T>(T[], T, Int32)

Searches for the specified object and returns the index of the last occurrence within the range of elements in the Array that extends from the first element to the specified index.

LastIndexOf<T>(T[], T, Int32, Int32)

Searches for the specified object and returns the index of the last occurrence within the range of elements in the Array that contains the specified number of elements and ends at the specified index.

LastIndexOf(Array, Object)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

Searches for the specified object and returns the index of the last occurrence within the entire one-dimensional Array.

C#
public static int LastIndexOf(Array array, object value);
C#
public static int LastIndexOf(Array array, object? value);

Parameters

array
Array

The one-dimensional Array to search.

value
Object

The object to locate in array.

Returns

The index of the last occurrence of value within the entire array, if found; otherwise, the lower bound of the array minus 1.

Exceptions

array is null.

array is multidimensional.

Examples

The following code example shows how to determine the index of the last occurrence of a specified element in an array.

C#
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(string), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );

// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintIndexAndValues( myArray );

// Searches for the last occurrence of the duplicated value.
string myString = "the";
int myIndex = Array.LastIndexOf( myArray, myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = Array.LastIndexOf( myArray, myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array.LastIndexOf( myArray, myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );

void PrintIndexAndValues( Array anArray )  {
   for ( int i = anArray.GetLowerBound(0); i <= anArray.GetUpperBound(0); i++ )
      Console.WriteLine( "\t[{0}]:\t{1}", i, anArray.GetValue( i ) );
}

/*
This code produces the following output.

The Array contains the following values:
   [0]:    the
   [1]:    quick
   [2]:    brown
   [3]:    fox
   [4]:    jumps
   [5]:    over
   [6]:    the
   [7]:    lazy
   [8]:    dog
   [9]:    in
   [10]:    the
   [11]:    barn
The last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/

Remarks

The one-dimensional Array is searched backward starting at the last element and ending at the first element.

The elements are compared to the specified value using the Object.Equals method. If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Since most arrays will have a lower bound of zero, this method would generally return -1 when value is not found. In the rare case that the lower bound of the array is equal to Int32.MinValue and value is not found, this method returns Int32.MaxValue, which is System.Int32.MinValue - 1.

This method is an O(n) operation, where n is the Length of array.

In .NET Framework 2.0 and later versions, this method uses the Equals and CompareTo methods of the Array to determine whether the Object specified by the value parameter exists. In earlier versions of .NET Framework, this determination was made by using the Equals and CompareTo methods of the value Object itself.

CompareTo methods of the item parameter on the objects in the collection.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

LastIndexOf(Array, Object, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

Searches for the specified object and returns the index of the last occurrence within the range of elements in the one-dimensional Array that extends from the first element to the specified index.

C#
public static int LastIndexOf(Array array, object value, int startIndex);
C#
public static int LastIndexOf(Array array, object? value, int startIndex);

Parameters

array
Array

The one-dimensional Array to search.

value
Object

The object to locate in array.

startIndex
Int32

The starting index of the backward search.

Returns

The index of the last occurrence of value within the range of elements in array that extends from the first element to startIndex, if found; otherwise, the lower bound of the array minus 1.

Exceptions

array is null.

startIndex is outside the range of valid indexes for array.

array is multidimensional.

Examples

The following code example shows how to determine the index of the last occurrence of a specified element in an array.

C#
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(string), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );

// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintIndexAndValues( myArray );

// Searches for the last occurrence of the duplicated value.
string myString = "the";
int myIndex = Array.LastIndexOf( myArray, myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = Array.LastIndexOf( myArray, myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array.LastIndexOf( myArray, myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );

void PrintIndexAndValues( Array anArray )  {
   for ( int i = anArray.GetLowerBound(0); i <= anArray.GetUpperBound(0); i++ )
      Console.WriteLine( "\t[{0}]:\t{1}", i, anArray.GetValue( i ) );
}

/*
This code produces the following output.

The Array contains the following values:
   [0]:    the
   [1]:    quick
   [2]:    brown
   [3]:    fox
   [4]:    jumps
   [5]:    over
   [6]:    the
   [7]:    lazy
   [8]:    dog
   [9]:    in
   [10]:    the
   [11]:    barn
The last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/

Remarks

The one-dimensional Array is searched backward starting at startIndex and ending at the first element.

The elements are compared to the specified value using the Object.Equals method. If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Since most arrays will have a lower bound of zero, this method would generally return -1 when value is not found. In the rare case that the lower bound of the array is equal to Int32.MinValue and value is not found, this method returns Int32.MaxValue, which is System.Int32.MinValue - 1.

This method is an O(n) operation, where n is the number of elements from the beginning of array to startIndex.

In .NET Framework 2.0 and later versions, this method uses the Equals and CompareTo methods of the Array to determine whether the Object specified by the value parameter exists. In earlier versions of .NET Framework, this determination was made by using the Equals and CompareTo methods of the value Object itself.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

LastIndexOf(Array, Object, Int32, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

Searches for the specified object and returns the index of the last occurrence within the range of elements in the one-dimensional Array that contains the specified number of elements and ends at the specified index.

C#
public static int LastIndexOf(Array array, object value, int startIndex, int count);
C#
public static int LastIndexOf(Array array, object? value, int startIndex, int count);

Parameters

array
Array

The one-dimensional Array to search.

value
Object

The object to locate in array.

startIndex
Int32

The starting index of the backward search.

count
Int32

The number of elements in the section to search.

Returns

The index of the last occurrence of value within the range of elements in array that contains the number of elements specified in count and ends at startIndex, if found; otherwise, the lower bound of the array minus 1.

Exceptions

array is null.

startIndex is outside the range of valid indexes for array.

-or-

count is less than zero.

-or-

startIndex and count do not specify a valid section in array.

array is multidimensional.

Examples

The following code example shows how to determine the index of the last occurrence of a specified element in an array. Note that the LastIndexOf method is a backward search; therefore, count must be less than or equal to (startIndex minus the lower bound of the array plus 1).

C#
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(string), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );

// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintIndexAndValues( myArray );

// Searches for the last occurrence of the duplicated value.
string myString = "the";
int myIndex = Array.LastIndexOf( myArray, myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = Array.LastIndexOf( myArray, myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array.LastIndexOf( myArray, myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );

void PrintIndexAndValues( Array anArray )  {
   for ( int i = anArray.GetLowerBound(0); i <= anArray.GetUpperBound(0); i++ )
      Console.WriteLine( "\t[{0}]:\t{1}", i, anArray.GetValue( i ) );
}

/*
This code produces the following output.

The Array contains the following values:
   [0]:    the
   [1]:    quick
   [2]:    brown
   [3]:    fox
   [4]:    jumps
   [5]:    over
   [6]:    the
   [7]:    lazy
   [8]:    dog
   [9]:    in
   [10]:    the
   [11]:    barn
The last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/

Remarks

The one-dimensional Array is searched backward starting at startIndex and ending at startIndex minus count plus 1, if count is greater than 0.

The elements are compared to the specified value using the Object.Equals method. If the element type is a nonintrinsic (user-defined) type, theEquals implementation of that type is used.

Since most arrays will have a lower bound of zero, this method would generally return -1 when value is not found. In the rare case that the lower bound of the array is equal to Int32.MinValue and value is not found, this method returns Int32.MaxValue, which is System.Int32.MinValue - 1.

This method is an O(n) operation, where n is count.

In .NET Framework 2.0 and later versions, this method uses the Equals and CompareTo methods of the Array to determine whether the Object specified by the value parameter exists. In earlier versions of .NET Framework, this determination was made by using the Equals and CompareTo methods of the value Object itself.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

LastIndexOf<T>(T[], T)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

Searches for the specified object and returns the index of the last occurrence within the entire Array.

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

Type Parameters

T

The type of the elements of the array.

Parameters

array
T[]

The one-dimensional, zero-based Array to search.

value
T

The object to locate in array.

Returns

The zero-based index of the last occurrence of value within the entire array, if found; otherwise, -1.

Exceptions

array is null.

Examples

The following code example demonstrates all three generic overloads of the LastIndexOf method. An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. The LastIndexOf<T>(T[], T) method overload searches the entire array from the end, and finds the second occurrence of the string. The LastIndexOf<T>(T[], T, Int32) method overload is used to search the array backward beginning with index location 3 and continuing to the beginning of the array, and finds the first occurrence of the string. Finally, the LastIndexOf<T>(T[], T, Int32, Int32) method overload is used to search a range of four entries, beginning at index location 4 and extending backward (that is, it searches the items at locations 4, 3, 2, and 1); this search returns -1 because there are no instances of the search string in that range.

C#
string[] dinosaurs = { "Tyrannosaurus",
    "Amargasaurus",
    "Mamenchisaurus",
    "Brachiosaurus",
    "Deinonychus",
    "Tyrannosaurus",
    "Compsognathus" };

Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
    Console.WriteLine(dinosaur);
}

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus"));

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3));

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/

Remarks

The Array is searched backward starting at the last element and ending at the first element.

The elements are compared to the specified value using the Object.Equals method. If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

This method is an O(n) operation, where n is the Length of array.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

LastIndexOf<T>(T[], T, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

Searches for the specified object and returns the index of the last occurrence within the range of elements in the Array that extends from the first element to the specified index.

C#
public static int LastIndexOf<T>(T[] array, T value, int startIndex);

Type Parameters

T

The type of the elements of the array.

Parameters

array
T[]

The one-dimensional, zero-based Array to search.

value
T

The object to locate in array.

startIndex
Int32

The zero-based starting index of the backward search.

Returns

The zero-based index of the last occurrence of value within the range of elements in array that extends from the first element to startIndex, if found; otherwise, -1.

Exceptions

array is null.

startIndex is outside the range of valid indexes for array.

Examples

The following code example demonstrates all three generic overloads of the LastIndexOf method. An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. The LastIndexOf<T>(T[], T) method overload searches the entire array from the end, and finds the second occurrence of the string. The LastIndexOf<T>(T[], T, Int32) method overload is used to search the array backward beginning with index location 3 and continuing to the beginning of the array, and finds the first occurrence of the string. Finally, the LastIndexOf<T>(T[], T, Int32, Int32) method overload is used to search a range of four entries, beginning at index location 4 and extending backward (that is, it searches the items at locations 4, 3, 2, and 1); this search returns -1 because there are no instances of the search string in that range.

C#
string[] dinosaurs = { "Tyrannosaurus",
    "Amargasaurus",
    "Mamenchisaurus",
    "Brachiosaurus",
    "Deinonychus",
    "Tyrannosaurus",
    "Compsognathus" };

Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
    Console.WriteLine(dinosaur);
}

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus"));

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3));

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/

Remarks

The Array is searched backward starting at startIndex and ending at the first element.

The elements are compared to the specified value using the Object.Equals method. If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

This method is an O(n) operation, where n is the number of elements from the beginning of array to startIndex.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

LastIndexOf<T>(T[], T, Int32, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

Searches for the specified object and returns the index of the last occurrence within the range of elements in the Array that contains the specified number of elements and ends at the specified index.

C#
public static int LastIndexOf<T>(T[] array, T value, int startIndex, int count);

Type Parameters

T

The type of the elements of the array.

Parameters

array
T[]

The one-dimensional, zero-based Array to search.

value
T

The object to locate in array.

startIndex
Int32

The zero-based starting index of the backward search.

count
Int32

The number of elements in the section to search.

Returns

The zero-based index of the last occurrence of value within the range of elements in array that contains the number of elements specified in count and ends at startIndex, if found; otherwise, -1.

Exceptions

array is null.

startIndex is outside the range of valid indexes for array.

-or-

count is less than zero.

-or-

startIndex and count do not specify a valid section in array.

Examples

The following code example demonstrates all three generic overloads of the LastIndexOf method. An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. The LastIndexOf<T>(T[], T) method overload searches the entire array from the end, and finds the second occurrence of the string. The LastIndexOf<T>(T[], T, Int32) method overload is used to search the array backward beginning with index location 3 and continuing to the beginning of the array, and finds the first occurrence of the string. Finally, the LastIndexOf<T>(T[], T, Int32, Int32) method overload is used to search a range of four entries, beginning at index location 4 and extending backward (that is, it searches the items at locations 4, 3, 2, and 1); this search returns -1 because there are no instances of the search string in that range.

C#
string[] dinosaurs = { "Tyrannosaurus",
    "Amargasaurus",
    "Mamenchisaurus",
    "Brachiosaurus",
    "Deinonychus",
    "Tyrannosaurus",
    "Compsognathus" };

Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
    Console.WriteLine(dinosaur);
}

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus"));

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3));

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/

Remarks

The Array is searched backward starting at startIndex and ending at startIndex minus count plus 1, if count is greater than 0.

The elements are compared to the specified value using the Object.Equals method. If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

This method is an O(n) operation, where n is count.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0