
You want to search your array of any type in the C# language, locating the offset of the value searched for. The IndexOf method on Array, in both its generic form and its LastIndexOf form, is one way you can do this.
Array.LastIndexOf TipsThese C# example programs use the Array.IndexOf method. They search an array.
The Array type is an abstract base type in the CLR, which means it can be used with instances of arrays in your program, such as int[]. However, you have to call the Array.IndexOf static method, as the method is not defined on specific array types. Here we see successful calls to Array.IndexOf.
Example code that uses IndexOf on Array [C#]
using System;
class Program
{
static void Main()
{
//
// Example integer array is declared.
//
int[] array = new int[6];
array[0] = 1;
array[1] = 3;
array[2] = 5;
array[3] = 7;
array[4] = 8;
array[5] = 5;
//
// Find index of element with value 5.
//
int index1 = Array.IndexOf(array, 5);
//
// Find index of value 3.
//
int index2 = Array.IndexOf<int>(array, 3);
//
// Find last index of 5.
//
int index3 = Array.LastIndexOf(array, 5);
//
// Write the results.
//
Console.WriteLine(index1);
Console.WriteLine(index2);
Console.WriteLine(index3);
}
}
Output
2
1
5
Notes. The Main entry point initializes an array of six integer elements. The array offset value of 5 is located; the result value is 2. Next, the Array<int> method is used. This method indicates that you want to find an element in an int array. The angle brackets indicate a type parameter on a generic method.
Notes on generic methods. You will find that generic methods in C# are used when you do not use the explicit <int> style syntax. The compiler is smart enough to infer what method you want to call from the parameters.
Here we look at a similar example that uses a string array and the Array.IndexOf method. Additionally, this method shows that the result of IndexOf is always -1 when the value is not found. The IndexOf method uses the default IEqualityComparer for the type, which means that string contents, not just references, are tested.
Another IndexOf example [C#]
using System;
class Program
{
static void Main()
{
//
// Example string array is declared.
//
string[] array = new string[6];
array[0] = null;
array[1] = "carrot";
array[2] = "rat";
array[3] = "";
array[4] = "carrot";
array[5] = "apple";
//
// Find string with this value starting at offset 2.
//
int index1 = Array.IndexOf(array, "carrot", 2, 3);
//
// Find a non-existent string.
//
int index2 = Array.IndexOf(array, "banana");
//
// Write the result.
//
Console.WriteLine(index1);
Console.WriteLine(index2);
}
}
Output
4
-1
Here we look at the performance of the IndexOf method on Arrays. Internally, the generic method has lots of error checking for invalid arguments, and calls into a virtual method depending on the type. The benchmark results show that using IndexOf<int> is unsuitable for performance work.
Array used in benchmark
static int[] _array = { 3, 5, 7, 9, 11, 13 };
Custom method tested
static int IndexOfInt(int[] arr, int value)
{
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == value)
{
return i;
}
}
return -1;
}
Method calls in loops (100000000 iterations)
int index = Array.IndexOf<int>(_array, 7);
int index = IndexOfInt(_array, 7);
Results
Array.IndexOf<int> method: 1.940 seconds
Custom method: 0.485 seconds [75% faster]
Here we saw several examples of using Array.IndexOf on elements that exist and do not exist, with code in the C# programming language. We saw the difference between generic methods and regular methods, and finally tested the Array.IndexOf method and found it to be much slower than searching with a custom method.
Array Types