Collection<T>.Add(T) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将对象添加到 Collection<T> 的结尾处。
public:
virtual void Add(T item);
public void Add (T item);
abstract member Add : 'T -> unit
override this.Add : 'T -> unit
Public Sub Add (item As T)
参数
- item
- T
要添加到 Collection<T> 末尾的对象。 对于引用类型,该值可以为 null
。
实现
示例
下面的代码示例演示 的许多属性和方法 Collection<T>。 该代码示例创建字符串集合,使用 Add 方法添加多个字符串,显示 Count,并列出字符串。 该示例使用 IndexOf 方法查找字符串的索引,并使用 Contains 方法来确定字符串是否在 集合中。 该示例使用 Insert 方法插入字符串,并使用 C#) 索引器 (默认 Item[] 属性检索和设置字符串。 该示例使用 方法按字符串标识删除字符串,使用 RemoveRemoveAt 方法按索引删除字符串。 最后, 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
注解
Collection<T> 接受 null
作为引用类型的有效值,并允许重复元素。
此方法是 O (1) 操作。