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
C# Linq Contains Method
The Contains() method in LINQ is used to check whether a sequence contains a specific element. It returns true if the element is found, otherwise false. This method works with any IEnumerable<T> collection including arrays, lists, and queryable sequences.
Syntax
Following is the syntax for using Contains() with collections −
bool result = collection.Contains(element);
Following is the syntax for using Contains() with queryable sequences −
bool result = collection.AsQueryable().Contains(element);
Parameters
- element − The value to locate in the sequence.
Return Value
Returns true if the source sequence contains an element that has the specified value; otherwise, false.
Using Contains() with String Arrays
Example
using System;
using System.Linq;
class Demo {
static void Main() {
string[] arr = { "Java", "C++", "Python" };
string str = "Python";
bool res = arr.Contains(str);
Console.WriteLine("Array contains Python? " + res);
bool notFound = arr.Contains("JavaScript");
Console.WriteLine("Array contains JavaScript? " + notFound);
}
}
The output of the above code is −
Array contains Python? True Array contains JavaScript? False
Using Contains() with Integer Lists
Example
using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
static void Main() {
List<int> numbers = new List<int> { 10, 20, 30, 40, 50 };
bool hasThirty = numbers.Contains(30);
Console.WriteLine("List contains 30? " + hasThirty);
bool hasSeventy = numbers.Contains(70);
Console.WriteLine("List contains 70? " + hasSeventy);
}
}
The output of the above code is −
List contains 30? True List contains 70? False
Using Contains() with Custom Objects
When using Contains() with custom objects, the method uses the default equality comparer. For reference types, this compares object references, not values −
Example
using System;
using System.Linq;
using System.Collections.Generic;
class Student {
public string Name { get; set; }
public int Age { get; set; }
public override bool Equals(object obj) {
if (obj is Student other)
return Name == other.Name && Age == other.Age;
return false;
}
public override int GetHashCode() {
return Name?.GetHashCode() ?? 0 ^ Age.GetHashCode();
}
}
class Demo {
static void Main() {
List<Student> students = new List<Student> {
new Student { Name = "Alice", Age = 20 },
new Student { Name = "Bob", Age = 22 }
};
Student searchStudent = new Student { Name = "Alice", Age = 20 };
bool found = students.Contains(searchStudent);
Console.WriteLine("Found Alice? " + found);
}
}
The output of the above code is −
Found Alice? True
Comparison: Contains() vs Any()
| Contains() | Any() |
|---|---|
| Checks for exact element match | Checks if any element satisfies a condition |
list.Contains(5) |
list.Any(x => x > 5) |
| Uses default equality comparison | Uses custom predicate function |
Conclusion
The LINQ Contains() method provides an efficient way to check if a collection contains a specific element. It works with all IEnumerable<T> collections and uses the default equality comparer, which can be customized by overriding Equals() and GetHashCode() methods in custom classes.
