Exists & Contains Method To Check An Item In A List Collection

Ever wondered which method to choose when one has to check if an element exists in the collection. Well, sooner or later all of us had the same question to ask ourselves and then we use Google, don’t we.

Today, I am going to explain which one to choose between Exists or Contain function when checking the existence of an item in a collection. Let’s take a deep dive into the ocean.

Exists and Contain methods look similar but have got different purpose.

Exists: Determine whether the List<T> contains elements that match the conditions defined by the specified predicate (Microsoft Documentation).

Contains: Determines whether an element is in the List<T>..(Microsoft Documentation).

The return value from both the functions is of type bool but their way of determining / checking the existence of an element is different.
Contain method determine equality by using the default equality comparer. That means with value type such as int, float, one should not implement IEquatable Interface but with reference types such as classes, one should implement IEquatable interface and should provide logic how two objects can be compared. For example, I have an employee class with two public properties; Name and Id. The class implements IEquatable Inteface and it also provides logic to compare two objects. In my case, two objects are said to be equal if both of them have same Name and Id.

  1. public class Employee: IEquatable < Employee >   
  2. {  
  3.     /// <summary>  
  4.     /// Name  
  5.     /// </summary>  
  6.     public string Name   
  7.     {  
  8.         get;  
  9.         set;  
  10.     }  
  11.   
  12.     /// <summary>  
  13.     /// Id  
  14.     /// </summary>  
  15.     public int Id   
  16.     {  
  17.         get;  
  18.         set;  
  19.     }  
  20.   
  21.     public override bool Equals(object other)   
  22.     {  
  23.         if (other == nullreturn false;  
  24.         return (this.Equals(other));  
  25.     }  
  26.   
  27.     /// <summary>  
  28.     /// hash function  
  29.     /// </summary>  
  30.     /// <returns></returns>  
  31.     public override int GetHashCode()   
  32.     {  
  33.         return Id;  
  34.     }  
  35.   
  36.     /// <summary>  
  37.     /// Equality comparator  
  38.     /// </summary>  
  39.     /// <param name="other">Object to be compared</param>  
  40.     /// <returns>true or false</returns>  
  41.     public bool Equals(Employee other)   
  42.     {  
  43.         if (other == nullreturn false;  
  44.   
  45.         Employee othEmployee = other as Employee;  
  46.   
  47.         if (othEmployee == nullreturn false;  
  48.   
  49.         if ((othEmployee.Id == this.Id) && (this.Name == othEmployee.Name))  
  50.         {  
  51.             return true;  
  52.         }  
  53.   
  54.         return false;  
  55.     }  
  56. }
When list.Contains(Employee emp) is called, equal function in the class operator checks if emp properties Name and Id matches with any item in the collection.

On the other hand, Exists method uses a predicate. Predicate represents a method with a set of criteria. Enumerates over the collection and determines if an element matches the criteria specified.
  1. /// <summary>  
  2. /// Predicate to chek if an employee  
  3. /// exists in the collection  
  4. /// </summary>  
  5. /// <param name="employee">Employee object</param>  
  6. /// <returns>Employee</returns>  
  7. private static Predicate < Employee > CheckNameAndId(Employee employee)   
  8. {  
  9.     return x = > x.Name == employee.Name && x.Id == employee.Id;  
  10. }  
  11. /// <summary>  
  12. /// Predicate to check if a name matches  
  13. /// </summary>  
  14. /// <param name="name"></param>  
  15. /// <returns></returns>  
  16. private static Predicate < Employee > CheckName(string name)  
  17. {  
  18.     return x = > x.Name == name;  
You can use Exists method with different types of Predicate, setting different criteria and checking collection against them.

I have attached code for your reference. Download and play around with it. Also, leave your comments if you have any questions.

Up Next
    Ebook Download
    View all
    Learn
    View all