C# | Removing first occurrence of object from Collection<T> Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report Collection<T>.Remove(T) is used to remove the first occurrence of a specific object from the Collection<T>. Syntax: public bool Remove (T item); Here, item is the object to remove from the Collection<T>. The value can be null for reference types. Return Value: True if item is successfully removed, otherwise, False. This method also returns False if item was not found in the original Collection<T>. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to remove the first // occurrence of a specific object // from 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>(); // Adding elements in Collection myColl myColl.Add("A"); myColl.Add("B"); myColl.Add("C"); myColl.Add("D"); myColl.Add("E"); // Displaying the number of elements in myColl Console.WriteLine("Count : " + myColl.Count); // Displaying the elements in myColl foreach(string str in myColl) { Console.WriteLine(str); } // Removing the first occurrence of // a specific object from the Collection myColl.Remove("C"); // Displaying the number of elements in myColl Console.WriteLine("Count : " + myColl.Count); // Displaying the elements in myColl foreach(string str in myColl) { Console.WriteLine(str); } } } Output: Count : 5 A B C D E Count : 4 A B D E Example 2: CSHARP // C# code to remove the first // occurrence of a specific object // from 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>(); // Adding elements in Collection myColl myColl.Add(2); myColl.Add(3); myColl.Add(4); myColl.Add(4); myColl.Add(5); // Displaying the number of elements in myColl Console.WriteLine("Count : " + myColl.Count); // Displaying the elements in myColl foreach(int i in myColl) { Console.WriteLine(i); } // Removing the first occurrence of // a specific object from the Collection myColl.Remove(4); // Displaying the number of elements in myColl Console.WriteLine("Count : " + myColl.Count); // Displaying the elements in myColl foreach(int i in myColl) { Console.WriteLine(i); } } } Output: Count : 5 2 3 4 4 5 Count : 4 2 3 4 5 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.remove?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Removing first occurrence of object from Collection<T> S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Collections.ObjectModel-Namespace CSharp-Collection-Class Similar Reads C# | Remove the first occurrence of a specific object from the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Remove(Object) method is used to remove the first occurrence of a 3 min read C# | Add an object to the end of Collection<T> Collection<T>.Add(T) method is used to add an object to the end of the Collection<T>. Syntax : public void Add (T item); Here, item is the object to be added to the end of the Collection<T>. The value can be null for reference types. Below given are some examples to understand the 2 min read C# | Remove all elements from the Collection<T> Collection<T>.Clear method is used to remove all elements from the Collection<T>. Syntax: public void Clear (); Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to remove all // elements from the Collection using System; using S 2 min read C# | Remove the first occurrence from the StringCollection StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace. StringCollection.Remove(String) method is used to remove the first occurrence of a specific string 3 min read C# | Searching the index of specified object in Collection<T> Collection<T>.IndexOf(T) method is used to search for the specified object and returns the zero-based index of the first occurrence within the entire Collection<T>. Syntax: public int IndexOf (T item); Here, item is the object to locate in the List<T>. The value can be null for ref 2 min read C# | First occurrence in the List that matches the specified conditions List<T>.Find(Predicate<T>) Method is used to search for an element which matches the conditions defined by the specified predicate and it returns the first occurrence of that element within the entire List<T>. Properties of List: It is different from the arrays. A list can be resiz 3 min read C# | Removing all nodes from LinkedList<T> LinkedList<T>.Clear method is used to remove the all nodes from the LinkedList<T>. Syntax: public void Clear (); Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to remove all // nodes from LinkedList using System; using System. 2 min read C# | Remove all objects from 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. Clear Method is used to remove the objects from the Queue. This 3 min read C# | Index of first occurrence in StringCollection StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace. StringCollection.IndexOf(String) method is used to search the specified string which returns the ze 2 min read C# | Intersection of SortedSet with a collection SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.IntersectWith(IEnumerable<T>) method is used to modify the current SortedSet<T> object so that it contains only elements that are also in a spe 2 min read Like