C# | Check if an element is in the Collection<T> Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report Collection<T>.Contains(T) method is used to determine whether an element is in the Collection<T>. Syntax: public bool Contains (T item); Here, item is the object to locate in the Collection<T>. The value can be null for reference types. Return Value: This method return True if item is found in the Collection<T>, otherwise, False. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to check if an // element is in the Collection using System; using System.Collections.Generic; using System.Collections.ObjectModel; class GFG { // Driver code public static void Main() { // Creating a collection of strings Collection<string> myColl = new Collection<string>(); myColl.Add("A"); myColl.Add("B"); myColl.Add("C"); myColl.Add("D"); myColl.Add("E"); // Checking if an element is in the Collection // The function returns "True" if the // item is present in Collection // else returns "False" Console.WriteLine(myColl.Contains("A")); } } Output: True Example 2: CSHARP // C# code to check if an // element is in the Collection using System; using System.Collections.Generic; using System.Collections.ObjectModel; class GFG { // Driver code public static void Main() { // Creating a collection of ints Collection<int> myColl = new Collection<int>(); myColl.Add(2); myColl.Add(3); myColl.Add(4); myColl.Add(5); // Checking if an element is in the Collection // The function returns "True" if the // item is present in Collection // else returns "False" Console.WriteLine(myColl.Contains(6)); } } Output: False Note: This method performs a linear search. Therefore, this method is an O(n) operation, where n is Count. Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.collection-1.contains?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Check if an element is in the Collection<T> S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Collections.ObjectModel-Namespace CSharp-Collection-Class Similar Reads C# | Check if an element is in the Queue Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque. Queue.Contains(T) Method is used to check whether an element is in the 2 min read C# | Insert an element into Collection<T> at specified index Collection<T>.Insert(Int32, T) method is used to insert an element into the Collection<T> at the specified index. Syntax: public void Insert (int index, T item); Parameters: index : The zero-based index at which item should be inserted. item : The object to insert. The value can be null 3 min read C# | Get or set the element at specified index in Collection<T> Collection<T>.Item[Int32] property is used to get or set the element at the specified index. Syntax: public T this[int index] { get; set; } Here, index is the zero-based index of the element to get or set. Return Value: The element at the specified index. Exception: This method will give Argum 3 min read C# | Check if HashSet and the specified collection contain the same elements Here's an example code that demonstrates how to use the Overlaps method to check if a HashSet and a specified collection share common elements in C#: C# using System; using System.Collections.Generic; class Program { static void Main(string[] args) { // Create a HashSet and a List HashSet<int> 3 min read C# | Check if a HashSet contains the specified element A HashSet is an unordered collection of the unique elements. It is found in System.Collections.Generic namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. As far as performance is concerned, it is better in comparison to the list. HashSet.C 2 min read Like