C# List Contains with Examples

How to use the List Contains method in C#

In this tutorial, you will learn how to use C# List Contains method to check whether a given element exists.

The Contains method is used to check for the presence of a specified element.

The method returns true if the element matches the element specified in the method argument or false if the element does not exist.

Learn more on Lists in C#

The following sample code illustrates the use of the List Contains method:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharp_List_Contains
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. List<string> vegetables = new List<string>();
  10. vegetables.Add("Broccoli");
  11. vegetables.Add("Carrot");
  12. vegetables.Add("Celery");
  13. vegetables.Add("Garlic");
  14. vegetables.Add("Chili pepper");
  15. string vegetable = "Garlic";
  16. if (vegetables.Contains(vegetable))
  17. {
  18. Console.WriteLine("{0} is included!", vegetable);
  19. }
  20. }
  21. }
  22. }

Output:

Garlic is included!

The List Contains method allows only one element to be specified; by concatenating Contains methods with AND and OR, it is possible to check whether multiple elements are included.

Using AND and Contains

The List Contains method allows only one element to be specified; by concatenating with AND, it is possible to check if multiple elements are included. An example in C# is shown below:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharp_List_Contains
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. List<string> vegetables = new List<string>();
  10. vegetables.Add("Broccoli");
  11. vegetables.Add("Carrot");
  12. vegetables.Add("Celery");
  13. vegetables.Add("Garlic");
  14. vegetables.Add("Chili pepper");
  15. string vegetable1 = "Garlic";
  16. string vegetable2 = "Celery";
  17. if (vegetables.Contains(vegetable1) && vegetables.Contains(vegetable2))
  18. {
  19. Console.WriteLine("{0} and {1} are included!", vegetable1, vegetable2);
  20. }
  21. }
  22. }
  23. }

Output:

Garlic and Celery are included!

Using OR and Contains

By concatenating with OR, it is possible to check if multiple elements are included. An example in C# is shown below:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharp_List_Contains
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. List<string> vegetables = new List<string>();
  10. vegetables.Add("Broccoli");
  11. vegetables.Add("Carrot");
  12. vegetables.Add("Celery");
  13. vegetables.Add("Garlic");
  14. vegetables.Add("Chili pepper");
  15. string vegetable1 = "Garlic";
  16. string vegetable2 = "Celery";
  17. if (vegetables.Contains(vegetable1) || vegetables.Contains(vegetable2))
  18. {
  19. Console.WriteLine("Either {0} or {1} is included!", vegetable1, vegetable2);
  20. }
  21. }
  22. }
  23. }

Output:

Either Garlic or Celery is included!

Using your own logic to check for the presence or absence of a match condition

The Contains method accepts the IEqualityComparer interface as its second argument, allowing you to create your own logic for match conditions by creating a class that implements the IEqualityComparer interface.

Let's look at the following example, which does not use the IEqualityComparer interface:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharp_List_Contains
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. List<string> vegetables = new List<string>();
  10. vegetables.Add("Broccoli");
  11. vegetables.Add("Carrot");
  12. vegetables.Add("Celery");
  13. vegetables.Add("Garlic");
  14. vegetables.Add("Chili pepper");
  15. var vegetable = "garlic";
  16. if (vegetables.Contains(vegetable))
  17. {
  18. Console.WriteLine("{0} is included!", vegetable);
  19. }
  20. else
  21. {
  22. Console.WriteLine("{0} not is included!", vegetable);
  23. }
  24. }
  25. }
  26. }

Output:

garlic not is included!

And here is an example of using the IEqualityComparer interface:

  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. namespace CSharp_List_Contains
  5. {
  6. public class Program
  7. {
  8. public static void Main(string[] args)
  9. {
  10. List<string> vegetables = new List<string>();
  11. vegetables.Add("Broccoli");
  12. vegetables.Add("Carrot");
  13. vegetables.Add("Celery");
  14. vegetables.Add("Garlic");
  15. vegetables.Add("Chili pepper");
  16. string vegetable = "garlic";
  17. ExampleEqualityComparer ec = new ExampleEqualityComparer();
  18. if (vegetables.Contains(vegetable, ec))
  19. {
  20. Console.WriteLine("{0} is included!", vegetable);
  21. }
  22. else
  23. {
  24. Console.WriteLine("{0} not is included!", vegetable);
  25. }
  26. }
  27. }
  28. public class ExampleEqualityComparer : IEqualityComparer<string>
  29. {
  30. public bool Equals(string x, string y)
  31. {
  32. if (x == null || y == null) return false;
  33. return (x.ToLower() == y.ToLower());
  34. }
  35. public int GetHashCode(string obj)
  36. {
  37. return obj.ToLower().GetHashCode();
  38. }
  39. }
  40. }

Output:

garlic is included!

Summary

In this tutorial, you have learned how to use the List Contains method to check whether a given element exists. 

If you want to apply your own logic to check, use IEqualityComparer as the second argument.


See also:
C# typeof with Examples
C# TryParse (int, double, float) with Examples
C# Data Types
C# Variables
C# IndexOf() – List, Array, ArrayList, String