C# ArrayList - Contains() Method



The C# ArrayList Contains() method is used to check whether an element is in the ArrayList.

This method serves a linear search operation, resulting in O(n) complexity, where n indicates the number of elements.

Syntax

Following is the syntax of the C# ArrayList Contains() method −

public virtual bool Contains (object? item);

Parameters

This method accepts an item (object) as a parameter to search in the ArrayList. The value can be null.

Return value

This method returns true if item is found in the ArrayList; Otherwise, false.

Example 1: Using the Contains() Method in ArrayList

Following is the basic example of the Contains() method to locate an element in the ArrayList −

    
using System;
using System.Collections;

class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { "A", "B", "C", "D" };
      string find = "B";
      
      bool res = arrayList.Contains(find);
      Console.WriteLine($"Is there '{find}' available in the list? {res}");
   }
}

Output

Following is the output −

Is there 'B' available in the list? True

Example 2: Find Different Types of Elements in ArrayList

Let's see another example of the Contains method to check whether an element is available in ArrayList −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      // Create an ArrayList with mixed data types
      ArrayList arrayList = new ArrayList { 1, 2, 3, "Hello", "World", 4.5 };

      int numberToFind = 3;
      bool numberResult = arrayList.Contains(numberToFind);
      Console.WriteLine($"Does the ArrayList contain the number {numberToFind}? {numberResult}");

      string stringToFind = "World";
      bool stringResult = arrayList.Contains(stringToFind);
      Console.WriteLine($"Does the ArrayList contain the string '{stringToFind}'? {stringResult}");
   }
}

Output

Following is the output −

Does the ArrayList contain the number 3? True
Does the ArrayList contain the string 'World'? True

Example 3: Check a Person in ArrayList

The below example creates a custom object. Here, we use the Contains() method to check whether the person details are available in the ArrayList −

using System;
using System.Collections;

class Person {
   public string Name {
      get;
      set;
   }
   public int Age {
      get;
      set;
   }

   public override bool Equals(object obj) {
      if (obj is Person other) {
         return this.Name == other.Name && this.Age == other.Age;
      }
      return false;
   }
   public override int GetHashCode()
   {
      return HashCode.Combine(Name, Age);
   }
}

class Program {
   static void Main() {
      // Create an ArrayList with custom objects
      ArrayList arrayList = new ArrayList {
         new Person {
            Name = "Aman", Age = 25
         },
         new Person {
            Name = "Akash", Age = 30
         }
      };

      // check for an object
      Person personToFind = new Person {
         Name = "Aman", Age = 25
      };
      bool personResult = arrayList.Contains(personToFind);
      Console.WriteLine($"Does the ArrayList contain {personToFind.Name}, age {personToFind.Age}? {personResult}");

   }
}

Output

Following is the output −

Does the ArrayList contain Aman, age 25? True
csharp_arraylist.htm
Advertisements