

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Removing first occurrence of object from Collection in C#
To remove the first occurrence of the object from Collection, the code is as follows −
Example
using System; using System.Collections.ObjectModel; public class Demo { public static void Main(){ Collection<string> col = new Collection<string>(); col.Add("Andy"); col.Add("Kevin"); col.Add("John"); col.Add("Nathan"); col.Add("Nathan"); col.Add("Katie"); col.Add("Barry"); col.Add("Nathan"); col.Add("Mark"); Console.WriteLine("Count of elements = "+ col.Count); Console.WriteLine("Iterating through the collection..."); var enumerator = col.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current); } col.Remove("Nathan"); Console.WriteLine("Count of elements (updated) = "+ col.Count); Console.WriteLine("Iterating through the collection... (updated)"); enumerator = col.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current); } } }
Output
This will produce the following output −
Count of elements = 9 Iterating through the collection... Andy Kevin John Nathan Nathan Katie Barry Nathan Mark Count of elements (updated) = 8 Iterating through the collection... (updated) Andy Kevin John Nathan Katie Barry Nathan Mark
Example
Let us see another example −
using System; using System.Collections.ObjectModel; public class Demo { public static void Main(){ Collection<string> col = new Collection<string>(); col.Add("One"); col.Add("Two"); col.Add("Two"); col.Add("Four"); col.Add("Five"); col.Add("Two"); col.Add("Six"); col.Add("Seven"); Console.WriteLine("Count of elements = "+ col.Count); Console.WriteLine("Iterating through the collection..."); var enumerator = col.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current); } col.Remove("Two"); Console.WriteLine("Count of elements = "+ col.Count); Console.WriteLine("Iterating through the collection... (updated)"); enumerator = col.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current); } } }
Output
This will produce the following output −
Count of elements = 8 Iterating through the collection... One Two Two Four Five Two Six Seven Count of elements = 7 Iterating through the collection... (updated) One Two Four Five Two Six Seven
- Related Questions & Answers
- Removing first occurrence of specified value from LinkedList in C#
- Remove the first occurrence of a specific object from the ArrayList in C#
- Removing an object in a child collection in MongoDB?
- Remove the first occurrence from the StringCollection in C#
- Index of first occurrence in StringCollection in C#?
- Removing an array element from a MongoDB collection
- Removing the odd occurrence of any number/element from an array in JavaScript
- Removing first k characters from string in JavaScript
- First occurrence of True number in Python
- Removing property from a JSON object in JavaScript
- Replace first occurrence of a character in Java
- Python - First occurrence of one list in another
- Removing an array element from MongoDB collection using update() and $pull
- Searching the index of specified object in Collection in C#
- Add an object to the end of Collection in C#
Advertisements