C# IndexOf() – List, Array, ArrayList, String

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:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharp_IndexOf_Sample
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. //Initialize
  10. string[] arr = new string[]
  11. {
  12. "Element 1", "Element 2", "Element 3",
  13. "Element 4", "Element 5", "Element 6"
  14. };
  15. string element = "Element 3";
  16. int index = Array.IndexOf(arr, element);
  17. //Print
  18. Console.WriteLine("Index of \"{0}\" is {1}", element, index);
  19. //
  20. element = "Element 8";
  21. index = Array.IndexOf(arr, element);
  22. //Print
  23. Console.WriteLine("Index of \"{0}\" is {1}", element, index);
  24. }
  25. }
  26. }

Output

Index of "Element 3" is 2
Index of "Element 8" is -1

ArrayList IndexOf

ArrayList instance.IndexOf(value)

Sample code:

  1. using System;
  2. using System.Collections;
  3. namespace CSharp_IndexOf_Sample
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. //Initialize
  10. ArrayList list = new ArrayList();
  11. //Add
  12. list.Add("1");
  13. list.Add("2");
  14. list.Add("3");
  15. list.Add("4");
  16. list.Add("5");
  17. string item = "0";
  18. int index = list.IndexOf(item);
  19. //Print
  20. Console.WriteLine("Index of \"{0}\" is {1}", item, index);
  21. //
  22. item = "5";
  23. index = list.IndexOf(item);
  24. //Print
  25. Console.WriteLine("Index of \"{0}\" is {1}", item, index);
  26. }
  27. }
  28. }

Output

Index of "0" is -1
Index of "5" is 4

List IndexOf

List.IndexOf

List<T> instance.IndexOf(value)

Sample code:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharp_IndexOf_Sample
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. //Initialize
  10. List<string> list = new List<string>()
  11. {
  12. "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"
  13. };
  14. string item = "Item 2";
  15. int index = list.IndexOf(item);
  16. //Print
  17. Console.WriteLine("Index of \"{0}\" is {1}", item, index);
  18. //
  19. item = "Item 7";
  20. index = list.IndexOf(item);
  21. //Print
  22. Console.WriteLine("Index of \"{0}\" is {1}", item, index);
  23. }
  24. }
  25. }

Output

Index of "Item 2" is 1
Index of "Item 7" is -1

String IndexOf

Sample code:

  1. using System;
  2. namespace CSharp_IndexOf_Sample
  3. {
  4. public class Program
  5. {
  6. public static void Main(string[] args)
  7. {
  8. string str = "Let's learn C# programming!";
  9. string substr = "programming";
  10. int index = str.IndexOf(substr);
  11. //Print
  12. Console.WriteLine("Index of \"{0}\" is {1}", substr, index);
  13. //
  14. substr = "Python";
  15. index = str.IndexOf(substr);
  16. //Print
  17. Console.WriteLine("Index of \"{0}\" is {1}", substr, index);
  18. }
  19. }
  20. }

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.


See also:
C# List Contains with Examples
C# typeof with Examples
C# TryParse (int, double, float) with Examples
C# Data Types
C# Variables