C# Array.IndexOf

IndexOf

Array.IndexOf searches an array. It acts upon an array of any type. It locates the offset of the value specified. The IndexOf method on Array, in both its generic form and its LastIndexOf form, is useful in many program contexts.

Array.LastIndexOf

Example

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[]. We have to call the Array.IndexOf static method. Here we see successful calls to Array.IndexOf.

Int ArrayStatic Method
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
Main method

In this example, we initialize an array of six integer elements. The array offset value of 5 is located—the result value is 2. Next, the IndexOf<int> method is used. This method indicates that you want to find an element in an int array.

Note:The angle brackets indicate a type parameter on a generic method. More information on generic methods is available.

Generic Method

Tip:You will find that generic methods in C# are used when you do not use the explicit <int> style syntax.

And:The compiler is smart enough to infer what method you want to call from the parameters.

Example 2

String type

Here we use a string array and the Array.IndexOf method. The result of IndexOf is -1 when the value is not found. The IndexOf method uses the default IEqualityComparer for the type.
String contents,
not just references,
are tested.

IEqualityComparer
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 nonexistent string.
	//
	int index2 = Array.IndexOf(array, "banana");

	//
	// Write the result.
	//
	Console.WriteLine(index1);
	Console.WriteLine(index2);
    }
}

Output

4
-1

Benchmark

Performance optimization

Next, 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.

Result:The benchmark results show that using IndexOf<int> is unsuitable for performance work.

Benchmark
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]

Summary

C# programming language

Here we saw 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.

Finally:We tested the Array.IndexOf method and found it to be much slower than searching with a custom method.


C#: Array