C# | Removing first occurrence of object from Collection<T>

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Count : 5
A
B
C
D
E
Count : 4
A
B
D
E

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


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:



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.