Check for the presence of a given element in an array or collection and return its index
This tutorial explains how to check whether an array, string, or collection contains an element that matches a specified value and determines its index.
You can use the IndexOf method to search for elements in an array or collection. If an element matching the value to be searched is found, it returns the first index number found. If not, it returns -1.
The IndexOf method is a static method for arrays. For collections, it can be used with the instance methods ArrayList and List.
Learn more:
Array IndexOf
Array.IndexOf(array, value)
Sample code:
using System; using System.Collections.Generic; namespace CSharp_IndexOf_Sample { public class Program { public static void Main(string[] args) { //Initialize string[] arr = new string[] { "Element 1", "Element 2", "Element 3", "Element 4", "Element 5", "Element 6" }; string element = "Element 3"; int index = Array.IndexOf(arr, element); Console.WriteLine("Index of \"{0}\" is {1}", element, index); // element = "Element 8"; index = Array.IndexOf(arr, element); Console.WriteLine("Index of \"{0}\" is {1}", element, index); } } }
Output
Index of "Element 3" is 2
Index of "Element 8" is -1
ArrayList IndexOf
ArrayList instance.IndexOf(value)
Sample code:
using System; using System.Collections; namespace CSharp_IndexOf_Sample { public class Program { public static void Main(string[] args) { //Initialize ArrayList list = new ArrayList(); //Add list.Add("1"); list.Add("2"); list.Add("3"); list.Add("4"); list.Add("5"); string item = "0"; int index = list.IndexOf(item); Console.WriteLine("Index of \"{0}\" is {1}", item, index); // item = "5"; index = list.IndexOf(item); Console.WriteLine("Index of \"{0}\" is {1}", item, index); } } }
Output
Index of "0" is -1
Index of "5" is 4
List IndexOf
List.IndexOf
List<T> instance.IndexOf(value)
Sample code:
using System; using System.Collections.Generic; namespace CSharp_IndexOf_Sample { public class Program { public static void Main(string[] args) { //Initialize List<string> list = new List<string>() { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6" }; string item = "Item 2"; int index = list.IndexOf(item); Console.WriteLine("Index of \"{0}\" is {1}", item, index); // item = "Item 7"; index = list.IndexOf(item); Console.WriteLine("Index of \"{0}\" is {1}", item, index); } } }
Output
Index of "Item 2" is 1
Index of "Item 7" is -1
String IndexOf
Sample code:
using System; namespace CSharp_IndexOf_Sample { public class Program { public static void Main(string[] args) { string str = "Let's learn C# programming!"; string substr = "programming"; int index = str.IndexOf(substr); Console.WriteLine("Index of \"{0}\" is {1}", substr, index); // substr = "Python"; index = str.IndexOf(substr); Console.WriteLine("Index of \"{0}\" is {1}", substr, index); } } }
Output
Index of "programming" is 15
Index of "Python" is -1
Summary
In this tutorial, you learned how to use the IndexOf method with examples of arrays, List, ArrayList, and strings.