Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check whether an element is contained in the ArrayList in C#
The ArrayList.Contains() method in C# checks whether a specific element exists in the ArrayList. This method returns a boolean value − true if the element is found, false otherwise.
Syntax
Following is the syntax for the Contains() method −
public virtual bool Contains(object item)
Parameters
-
item − The object to locate in the ArrayList. The value can be null.
Return Value
Returns true if the item is found in the ArrayList; otherwise, false.
Using Contains() with String Elements
The following example demonstrates checking for string elements in an ArrayList −
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList list = new ArrayList();
list.Add("One");
list.Add("Two");
list.Add("Three");
list.Add("Four");
list.Add("Five");
list.Add("Six");
list.Add("Seven");
list.Add("Eight");
Console.WriteLine("ArrayList elements...");
foreach(string str in list) {
Console.WriteLine(str);
}
Console.WriteLine("ArrayList is read-only? = " + list.IsReadOnly);
Console.WriteLine("Does the element 'Six' exist in the ArrayList? = " + list.Contains("Six"));
Console.WriteLine("Does the element 'Nine' exist in the ArrayList? = " + list.Contains("Nine"));
}
}
The output of the above code is −
ArrayList elements... One Two Three Four Five Six Seven Eight ArrayList is read-only? = False Does the element 'Six' exist in the ArrayList? = True Does the element 'Nine' exist in the ArrayList? = False
Using Contains() with Numeric Elements
The following example shows how to check for numeric elements in an ArrayList −
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add(100);
arrList.Add(200);
arrList.Add(300);
arrList.Add(400);
arrList.Add(500);
Console.WriteLine("Display elements in a range...");
IEnumerator demoEnum = arrList.GetEnumerator(1, 3);
while (demoEnum.MoveNext()) {
Object ob = demoEnum.Current;
Console.WriteLine(ob);
}
Console.WriteLine("Does the element 300 exist in the ArrayList? = " + arrList.Contains(300));
Console.WriteLine("Does the element 1000 exist in the ArrayList? = " + arrList.Contains(1000));
}
}
The output of the above code is −
Display elements in a range... 200 300 400 Does the element 300 exist in the ArrayList? = True Does the element 1000 exist in the ArrayList? = False
How It Works
The Contains() method uses the Equals() method to compare each element in the ArrayList with the specified object. It performs a linear search through the collection, checking each element sequentially until a match is found or all elements have been examined.
Key Rules
-
The method performs case-sensitive comparison for string elements.
-
It can search for
nullvalues if the ArrayList contains null elements. -
The search operation has O(n) time complexity where n is the number of elements.
-
For custom objects, ensure the
Equals()method is properly overridden for accurate comparison.
Conclusion
The ArrayList.Contains() method provides a simple way to check if an element exists in an ArrayList. It returns a boolean value and works with any object type, making it useful for validation and conditional operations in your C# applications.
