How to check whether a List contains a specified element in C#?

How to check whether a List contains a specified element in C#?  While looking for a solution to this inquiry, I’ve run into comparative ones using LINQ yet I haven’t had the option to completely get them (and accordingly, execute them), as I’m not acclimated with it. What I might want to, fundamentally, is this:

List is an assortment of things/components. You can check assuming the rundown contains a thing or an article is available in the rundown. In this instructional exercise, we will figure out how to check if a thing/component is available in a given List.

List<T>.Contains(T) Method is utilized to check whether or not a component is in the List<T>.

Properties of List:

  • It is unique in relation to the exhibits. A rundown can be resized progressively however clusters can’t.
  • List class can acknowledge invalid as a legitimate incentive for reference types and it likewise permits copy components.
  • In the event that the Count becomes equivalents to Capacity, the limit of the List increments consequently by redistributing the inward cluster. The current components will be duplicated to the new cluster before the expansion of the new component.

Syntax:

public bool Contains (T item);

Here, thing is the item which is to be situate in the List<T>. The worth can be invalid for reference types.

Bring Value back: This strategy returns True in the event that the thing is found in the List<T> in any case gets back False.

Underneath programs represent the utilization of List<T>.Contains(T) Method:

Example 1:

// 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));
    }
}

Output:

True

Example 2:

// 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"));
    }
}

Output:

False

Leave a Reply

Your email address will not be published. Required fields are marked *