C# | How to check whether a List contains a specified element

List<T>.Contains(T) Method is used to check whether an element is in the List<T> or not.

Properties of List:

  • It is different from the arrays. A list can be resized dynamically but arrays cannot.
  • List class can accept null as a valid value for reference types and it also allows duplicate elements.
  • If the Count becomes equals to Capacity then the capacity of the List increases automatically by reallocating the internal array. The existing elements will be copied to the new array before the addition of the new element.

Syntax:

public bool Contains (T item);

Here, item is the object which is to be locate in the List<T>. The value can be null for reference types.

Return Value: This method returns True if the item is found in the List<T> otherwise returns False.

Below programs illustrate the use of List<T>.Contains(T) Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to check whether the
// element is present in the List
// or not
using System;
using System.Collections;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating an List<T> of Integers
        List<int> firstlist = new List<int>();
  
        // Adding elements to List
        firstlist.Add(1);
        firstlist.Add(2);
        firstlist.Add(3);
        firstlist.Add(4);
        firstlist.Add(5);
        firstlist.Add(6);
        firstlist.Add(7);
  
        // Checking whether 4 is present
        // in List or not
        Console.Write(firstlist.Contains(4));
    }
}

chevron_right


Output:

True

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to check whether the
// element is present in the List
// or not
using System;
using System.Collections;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating an List<T> of String
        List<String> firstlist = new List<String>();
  
        // Adding elements to List
        firstlist.Add("Geeks");
        firstlist.Add("For");
        firstlist.Add("Geeks");
        firstlist.Add("GFG");
        firstlist.Add("C#");
        firstlist.Add("Tutorials");
        firstlist.Add("GeeksforGeeks");
  
        // Checking whether Java is present
        // in List or not
        Console.Write(firstlist.Contains("Java"));
    }
}

chevron_right


Output:

False

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.