共用方式為


Collection<T>.Remove(T) 方法

定義

Collection<T> 移除特定物件之第一個符合的元素。

public:
 virtual bool Remove(T item);
public bool Remove (T item);
abstract member Remove : 'T -> bool
override this.Remove : 'T -> bool
Public Function Remove (item As T) As Boolean

參數

item
T

要從 Collection<T> 移除的物件。 參考類型的值可以是 null

傳回

如果成功移除 item 則為 true,否則為 false。 如果在原始的 false 中找不到 item,則這個方法也會傳回 Collection<T>

實作

範例

下列程式代碼範例示範 的許多 屬性和方法 Collection<T>。 程式代碼範例會建立字串集合、使用 Add 方法來加入數個字串、顯示 Count和 列出字串。 此範例會 IndexOf 使用 方法來尋找字串的索引,以及 Contains 方法來判斷字串是否在集合中。 此範例會使用 Insert 方法插入字串,並使用預設 Item[] 屬性擷取和設定字串, (C# ) 中的索引器。 此範例會使用 Remove 方法和 方法的索引 RemoveAt ,依字串識別移除字串。 最後, Clear 方法是用來清除集合中的所有字串。

using namespace System;
using namespace System::Collections::Generic;
using namespace System::Collections::ObjectModel;

public ref class Demo
{
public:
    static void Main()
    {
        Collection<String^>^ dinosaurs = gcnew Collection<String^>();

        dinosaurs->Add("Psitticosaurus");
        dinosaurs->Add("Caudipteryx");
        dinosaurs->Add("Compsognathus");
        dinosaurs->Add("Muttaburrasaurus");

        Console::WriteLine("{0} dinosaurs:", dinosaurs->Count);
        Display(dinosaurs);

        Console::WriteLine("\nIndexOf(\"Muttaburrasaurus\"): {0}",
            dinosaurs->IndexOf("Muttaburrasaurus"));

        Console::WriteLine("\nContains(\"Caudipteryx\"): {0}",
            dinosaurs->Contains("Caudipteryx"));

        Console::WriteLine("\nInsert(2, \"Nanotyrannus\")");
        dinosaurs->Insert(2, "Nanotyrannus");
        Display(dinosaurs);

        Console::WriteLine("\ndinosaurs[2]: {0}", dinosaurs[2]);

        Console::WriteLine("\ndinosaurs[2] = \"Microraptor\"");
        dinosaurs[2] = "Microraptor";
        Display(dinosaurs);

        Console::WriteLine("\nRemove(\"Microraptor\")");
        dinosaurs->Remove("Microraptor");
        Display(dinosaurs);

        Console::WriteLine("\nRemoveAt(0)");
        dinosaurs->RemoveAt(0);
        Display(dinosaurs);

        Console::WriteLine("\ndinosaurs.Clear()");
        dinosaurs->Clear();
        Console::WriteLine("Count: {0}", dinosaurs->Count);
    }

private:
    static void Display(Collection<String^>^ cs)
    {
        Console::WriteLine();
        for each( String^ item in cs )
        {
            Console::WriteLine(item);
        }
    }
};

int main()
{
    Demo::Main();
}

/* This code example produces the following output:

4 dinosaurs:

Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus

IndexOf("Muttaburrasaurus"): 3

Contains("Caudipteryx"): True

Insert(2, "Nanotyrannus")

Psitticosaurus
Caudipteryx
Nanotyrannus
Compsognathus
Muttaburrasaurus

dinosaurs[2]: Nanotyrannus

dinosaurs[2] = "Microraptor"

Psitticosaurus
Caudipteryx
Microraptor
Compsognathus
Muttaburrasaurus

Remove("Microraptor")

Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus

RemoveAt(0)

Caudipteryx
Compsognathus
Muttaburrasaurus

dinosaurs.Clear()
Count: 0
 */
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

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

        dinosaurs.Add("Psitticosaurus");
        dinosaurs.Add("Caudipteryx");
        dinosaurs.Add("Compsognathus");
        dinosaurs.Add("Muttaburrasaurus");

        Console.WriteLine("{0} dinosaurs:", dinosaurs.Count);
        Display(dinosaurs);

        Console.WriteLine("\nIndexOf(\"Muttaburrasaurus\"): {0}",
            dinosaurs.IndexOf("Muttaburrasaurus"));

        Console.WriteLine("\nContains(\"Caudipteryx\"): {0}",
            dinosaurs.Contains("Caudipteryx"));

        Console.WriteLine("\nInsert(2, \"Nanotyrannus\")");
        dinosaurs.Insert(2, "Nanotyrannus");
        Display(dinosaurs);

        Console.WriteLine("\ndinosaurs[2]: {0}", dinosaurs[2]);

        Console.WriteLine("\ndinosaurs[2] = \"Microraptor\"");
        dinosaurs[2] = "Microraptor";
        Display(dinosaurs);

        Console.WriteLine("\nRemove(\"Microraptor\")");
        dinosaurs.Remove("Microraptor");
        Display(dinosaurs);

        Console.WriteLine("\nRemoveAt(0)");
        dinosaurs.RemoveAt(0);
        Display(dinosaurs);

        Console.WriteLine("\ndinosaurs.Clear()");
        dinosaurs.Clear();
        Console.WriteLine("Count: {0}", dinosaurs.Count);
    }

    private static void Display(Collection<string> cs)
    {
        Console.WriteLine();
        foreach( string item in cs )
        {
            Console.WriteLine(item);
        }
    }
}

/* This code example produces the following output:

4 dinosaurs:

Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus

IndexOf("Muttaburrasaurus"): 3

Contains("Caudipteryx"): True

Insert(2, "Nanotyrannus")

Psitticosaurus
Caudipteryx
Nanotyrannus
Compsognathus
Muttaburrasaurus

dinosaurs[2]: Nanotyrannus

dinosaurs[2] = "Microraptor"

Psitticosaurus
Caudipteryx
Microraptor
Compsognathus
Muttaburrasaurus

Remove("Microraptor")

Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus

RemoveAt(0)

Caudipteryx
Compsognathus
Muttaburrasaurus

dinosaurs.Clear()
Count: 0
 */
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Public Class Demo
    
    Public Shared Sub Main() 

        Dim dinosaurs As New Collection(Of String)

        dinosaurs.Add("Psitticosaurus")
        dinosaurs.Add("Caudipteryx")
        dinosaurs.Add("Compsognathus")
        dinosaurs.Add("Muttaburrasaurus")

        Console.WriteLine("{0} dinosaurs:", dinosaurs.Count)
        Display(dinosaurs)
    
        Console.WriteLine(vbLf & "IndexOf(""Muttaburrasaurus""): {0}", _
            dinosaurs.IndexOf("Muttaburrasaurus"))

        Console.WriteLine(vbLf & "Contains(""Caudipteryx""): {0}", _
            dinosaurs.Contains("Caudipteryx"))

        Console.WriteLine(vbLf & "Insert(2, ""Nanotyrannus"")")
        dinosaurs.Insert(2, "Nanotyrannus")
        Display(dinosaurs)

        Console.WriteLine(vbLf & "dinosaurs(2): {0}", dinosaurs(2))

        Console.WriteLine(vbLf & "dinosaurs(2) = ""Microraptor""")
        dinosaurs(2) = "Microraptor"
        Display(dinosaurs)

        Console.WriteLine(vbLf & "Remove(""Microraptor"")")
        dinosaurs.Remove("Microraptor")
        Display(dinosaurs)

        Console.WriteLine(vbLf & "RemoveAt(0)")
        dinosaurs.RemoveAt(0)
        Display(dinosaurs)
 
        Console.WriteLine(vbLf & "dinosaurs.Clear()")
        dinosaurs.Clear()
        Console.WriteLine("Count: {0}", dinosaurs.Count)

    End Sub
    
    Private Shared Sub Display(ByVal cs As Collection(Of String)) 
        Console.WriteLine()
        For Each item As String In cs
            Console.WriteLine(item)
        Next item
    End Sub
End Class

' This code example produces the following output:
'
'4 dinosaurs:
'
'Psitticosaurus
'Caudipteryx
'Compsognathus
'Muttaburrasaurus
'
'IndexOf("Muttaburrasaurus"): 3
'
'Contains("Caudipteryx"): True
'
'Insert(2, "Nanotyrannus")
'
'Psitticosaurus
'Caudipteryx
'Nanotyrannus
'Compsognathus
'Muttaburrasaurus
'
'dinosaurs(2): Nanotyrannus
'
'dinosaurs(2) = "Microraptor"
'
'Psitticosaurus
'Caudipteryx
'Microraptor
'Compsognathus
'Muttaburrasaurus
'
'Remove("Microraptor")
'
'Psitticosaurus
'Caudipteryx
'Compsognathus
'Muttaburrasaurus
'
'RemoveAt(0)
'
'Caudipteryx
'Compsognathus
'Muttaburrasaurus
'
'dinosaurs.Clear()
'Count: 0

備註

這個方法會使用 清單中值類型的預設相等比較子EqualityComparer<T>.DefaultT來決定相等。

此方法會執行線性搜尋;因此,平均運行時間會與 Count成正比。 也就是說,這個方法是 O () n 作業,其中 nCount

給繼承者的注意事項

衍生類別可以覆寫 RemoveItem(Int32) 以變更此方法的行為。

適用於

另請參閱