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

Introduction

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 Contains methods

Exists and Contains methods look similar but have got different purposes.

  • 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 determines equality by using the default equality comparer. That means with value types such as int and float, one should not implement IEquatable Interface, but with reference types such as classes, one should implement the IEquatable interface and should provide logic on how two objects can be compared. For example, I have an employee class with two public properties; Name and Id. The class implements IEquatable Interface, and it also provides logic to compare two objects. In my case, two objects are said to be equal if both of them have the same Name and Id.

public class Employee : IEquatable<Employee>
{
    public string Name { get; set; }
    public int Id { get; set; }
    public override bool Equals(object other)
    {
        if (other == null)
            return false;  
        if (ReferenceEquals(this, other))
            return true;
        if (other is Employee otherEmployee)
        {
            return Equals(otherEmployee);
        }
        return false;
    }
    public override int GetHashCode()
    {
        return Id;
    }
    public bool Equals(Employee other)
    {
        if (other == null)
            return false;
        return (other.Id == this.Id) && (other.Name == this.Name);
    }
}

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 the collection and determines if an element matches the criteria specified.

private static Predicate<Employee> CheckNameAndId(Employee employee)
{
    return x => x.Name == employee.Name && x.Id == employee.Id;
}
private static Predicate<Employee> CheckName(string name)
{
    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.


Recommended Free Ebook
Similar Articles