Collection<T>.Item[Int32] 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置指定索引处的元素。
public:
property T default[int] { T get(int index); void set(int index, T value); };
public T this[int index] { get; set; }
member this.Item(int) : 'T with get, set
Default Public Property Item(index As Integer) As T
参数
- index
- Int32
要获取或设置的元素的从零开始的索引。
属性值
T
指定索引处的元素。
实现
例外
示例
下面的代码示例演示 的许多属性和方法 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
作为引用类型的有效值,并允许重复元素。
通过此属性,可以使用以下语法来访问集合中的特定元素:myCollection[index]
。
C# 语言使用此关键字 (keyword) 来定义索引器,而不是实现 Item[] 属性。 Visual Basic 将 Item[] 实现为默认属性,该属性提供相同的索引功能。
检索此属性的值是 O (1) 操作;设置 属性也是 O (1) 操作。
继承者说明
派生类可以重写 SetItem(Int32, T) 以更改设置此属性的行为。