C# Collection Remove

Description

removes the first occurrence of a specific object from the Collection.

Syntax


public bool Remove(
  T item
)

Parameters

  • item - The object to remove from the Collection . The value can be null for reference types.

Returns

returns true if item is successfully removed; otherwise, false. This method also returns false if item was not found in the original Collection.

Example

The following code example demonstrates many of the properties and methods of Collection.


using System;/*from  w ww  .  ja va  2 s. c  om*/
using System.Collections.Generic;
using System.Collections.ObjectModel;

public class Demo
{
    public static void Main()
    {
        Collection<string> myData = new Collection<string>();

        myData.Add("A");
        myData.Add("B");
        myData.Add("C");
        myData.Add("D");


        myData.Remove("A");

        foreach( string item in myData )
        {
            Console.WriteLine(item);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections.ObjectModel »




Collection