C# – Check if Element is present in List

List is a collection of items/elements. You can check if the list contains an item or an object is present in the list.

In this tutorial, we shall learn how to check if an item/element is present in a given List.

To check if an element is present in the list, use List.Contains() method. The definition of List.Contains() method is given below.

</>
Copy
bool List<int>.Contains(int item)

If given element is present in the list, then List.Contains() returns True, else, it returns False.

Example 1 – Check if Element is in C# List using Contains()

In the following program, we have a list of integers.

We shall check if element 68 is present in the list or not using Contains() method. As 68 is present in the list, List.Contains() method returns True.

Then we shall check if the element 59 is present in the list. As 59 is not present in the list, List.Contains() method returns false.

Program.cs

</>
Copy
using System;
using System.Collections.Generic;

class Program {
    static void Main(string[] args) {
        //create list
        List<int> nums = new List<int>();
        nums.Add(52);
        nums.Add(68);
        nums.Add(73);

        //check if element is present in the list
        bool isElementPresent = nums.Contains(68);
        Console.WriteLine("68 present in the list : "+isElementPresent);

        //check if element is present in the list
        isElementPresent = nums.Contains(59);
        Console.WriteLine("59 present in the list : "+isElementPresent);
    }
}

Run the above C# program.

Output

68 present in the list : True
59 present in the list : False

Example 2 – Check if Object is present in the C# List

In this example, we shall check if a given object is present in the list.

When working with objects, List.Contains() method considers that two objects are equal only if they refer to same object instance. Two instances with same values for its properties is not considered equal.

Program.cs

</>
Copy
using System;
using System.Collections.Generic;

class Program {
    static void Main(string[] args) {
        //create list
        List<Car> cars = new List<Car>();
        //add objects to the list
        cars.Add(new Car("Toyota", 1250000));
        cars.Add(new Car("Tata", 1300000));
        cars.Add(new Car("Honda", 1150000));
        
        //create a car object
        Car mycar = new Car("Tata", 1300000);

        // check if mycar is present in the list
        bool isCarPresent = cars.Contains(mycar);
        Console.WriteLine(isCarPresent);
    }
}

class Car{
    public string name;
    public int price;
    public Car(string name, int price){
        this.name = name;
        this.price = price;
    }
}

Run the C# program.

Output

False

The values for properties are same, but the object instance is not the same.

Let us rewrite the above program and modify as shown below.

Program.cs

</>
Copy
using System;
using System.Collections.Generic;

class Program {
    static void Main(string[] args) {
        //create list
        List<Car> cars = new List<Car>();
        //add objects to the list
        Car car1 = new Car("Toyota", 1250000);
        cars.Add(car1);
        Car car2 = new Car("Tata", 1300000);
        cars.Add(car2);
        Car car3 = new Car("Honda", 1150000);
        cars.Add(car3);
        
        // check if car2 is present in the list
        bool isCarPresent = cars.Contains(car2);
        Console.WriteLine(isCarPresent);
    }
}

class Car{
    public string name;
    public int price;
    public Car(string name, int price){
        this.name = name;
        this.price = price;
    }
}

Run the above C# program.

True

In the above program, we have used the same instance, that we added to the list, to check with Contains() method. So, Contains() method, when comparing the objects in the list, finds a match for the given object.

Conclusion

In this C# Tutorial, we learned how to check if an element or object is present in the list or not using List.Contains() method.