ReadOnlyCollection<T>.GetEnumerator 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回循环访问 ReadOnlyCollection<T> 的枚举数。
public:
virtual System::Collections::Generic::IEnumerator<T> ^ GetEnumerator();
public System.Collections.Generic.IEnumerator<T> GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.Generic.IEnumerator<'T>
override this.GetEnumerator : unit -> System.Collections.Generic.IEnumerator<'T>
Public Function GetEnumerator () As IEnumerator(Of T)
返回
IEnumerator<T> 的一个 ReadOnlyCollection<T>。
实现
示例
下面的代码示例使用 枚举器显示包装 的 ReadOnlyCollection<T>List<T>的内容。 在 C++) 中,for each
枚举器被 foreach
Visual Basic 中的 语句 (For Each
隐藏。
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Collections::ObjectModel;
void main()
{
List<String^>^ dinosaurs = gcnew List<String^>();
dinosaurs->Add("Tyrannosaurus");
dinosaurs->Add("Amargasaurus");
dinosaurs->Add("Deinonychus");
dinosaurs->Add("Compsognathus");
ReadOnlyCollection<String^>^ readOnlyDinosaurs =
gcnew ReadOnlyCollection<String^>(dinosaurs);
Console::WriteLine();
for each(String^ dinosaur in readOnlyDinosaurs )
{
Console::WriteLine(dinosaur);
}
Console::WriteLine("\nCount: {0}", readOnlyDinosaurs->Count);
Console::WriteLine("\nContains(\"Deinonychus\"): {0}",
readOnlyDinosaurs->Contains("Deinonychus"));
Console::WriteLine("\nreadOnlyDinosaurs[3]: {0}",
readOnlyDinosaurs[3]);
Console::WriteLine("\nIndexOf(\"Compsognathus\"): {0}",
readOnlyDinosaurs->IndexOf("Compsognathus"));
Console::WriteLine("\nInsert into the wrapped List:");
Console::WriteLine("Insert(2, \"Oviraptor\")");
dinosaurs->Insert(2, "Oviraptor");
Console::WriteLine();
for each( String^ dinosaur in readOnlyDinosaurs )
{
Console::WriteLine(dinosaur);
}
array<String^>^ dinoArray =
gcnew array<String^>(readOnlyDinosaurs->Count + 2);
readOnlyDinosaurs->CopyTo(dinoArray, 1);
Console::WriteLine("\nCopied array has {0} elements:",
dinoArray->Length);
for each( String^ dinosaur in dinoArray )
{
Console::WriteLine("\"{0}\"", dinosaur);
}
}
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Deinonychus
Compsognathus
Count: 4
Contains("Deinonychus"): True
readOnlyDinosaurs[3]: Compsognathus
IndexOf("Compsognathus"): 3
Insert into the wrapped List:
Insert(2, "Oviraptor")
Tyrannosaurus
Amargasaurus
Oviraptor
Deinonychus
Compsognathus
Copied array has 7 elements:
""
"Tyrannosaurus"
"Amargasaurus"
"Oviraptor"
"Deinonychus"
"Compsognathus"
""
*/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
public class Example
{
public static void Main()
{
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Tyrannosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Compsognathus");
ReadOnlyCollection<string> readOnlyDinosaurs =
new ReadOnlyCollection<string>(dinosaurs);
Console.WriteLine();
foreach( string dinosaur in readOnlyDinosaurs )
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nCount: {0}", readOnlyDinosaurs.Count);
Console.WriteLine("\nContains(\"Deinonychus\"): {0}",
readOnlyDinosaurs.Contains("Deinonychus"));
Console.WriteLine("\nreadOnlyDinosaurs[3]: {0}",
readOnlyDinosaurs[3]);
Console.WriteLine("\nIndexOf(\"Compsognathus\"): {0}",
readOnlyDinosaurs.IndexOf("Compsognathus"));
Console.WriteLine("\nInsert into the wrapped List:");
Console.WriteLine("Insert(2, \"Oviraptor\")");
dinosaurs.Insert(2, "Oviraptor");
Console.WriteLine();
foreach( string dinosaur in readOnlyDinosaurs )
{
Console.WriteLine(dinosaur);
}
string[] dinoArray = new string[readOnlyDinosaurs.Count + 2];
readOnlyDinosaurs.CopyTo(dinoArray, 1);
Console.WriteLine("\nCopied array has {0} elements:",
dinoArray.Length);
foreach( string dinosaur in dinoArray )
{
Console.WriteLine("\"{0}\"", dinosaur);
}
}
}
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Deinonychus
Compsognathus
Count: 4
Contains("Deinonychus"): True
readOnlyDinosaurs[3]: Compsognathus
IndexOf("Compsognathus"): 3
Insert into the wrapped List:
Insert(2, "Oviraptor")
Tyrannosaurus
Amargasaurus
Oviraptor
Deinonychus
Compsognathus
Copied array has 7 elements:
""
"Tyrannosaurus"
"Amargasaurus"
"Oviraptor"
"Deinonychus"
"Compsognathus"
""
*/
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Public Class Example
Public Shared Sub Main()
Dim dinosaurs As New List(Of String)
dinosaurs.Add("Tyrannosaurus")
dinosaurs.Add("Amargasaurus")
dinosaurs.Add("Deinonychus")
dinosaurs.Add("Compsognathus")
Dim readOnlyDinosaurs As _
New ReadOnlyCollection(Of String)(dinosaurs)
Console.WriteLine()
For Each dinosaur As String In readOnlyDinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & "Count: {0}", _
readOnlyDinosaurs.Count)
Console.WriteLine(vbLf & "Contains(""Deinonychus""): {0}", _
readOnlyDinosaurs.Contains("Deinonychus"))
Console.WriteLine(vbLf & _
"readOnlyDinosaurs(3): {0}", readOnlyDinosaurs(3))
Console.WriteLine(vbLf & "IndexOf(""Compsognathus""): {0}", _
readOnlyDinosaurs.IndexOf("Compsognathus"))
Console.WriteLine(vbLf & "Insert into the wrapped List:")
Console.WriteLine("Insert(2, ""Oviraptor"")")
dinosaurs.Insert(2, "Oviraptor")
Console.WriteLine()
For Each dinosaur As String In readOnlyDinosaurs
Console.WriteLine(dinosaur)
Next
Dim dinoArray(readOnlyDinosaurs.Count + 1) As String
readOnlyDinosaurs.CopyTo(dinoArray, 1)
Console.WriteLine(vbLf & "Copied array has {0} elements:", _
dinoArray.Length)
For Each dinosaur As String In dinoArray
Console.WriteLine("""{0}""", dinosaur)
Next
End Sub
End Class
' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Deinonychus
'Compsognathus
'
'Count: 4
'
'Contains("Deinonychus"): True
'
'readOnlyDinosaurs(3): Compsognathus
'
'IndexOf("Compsognathus"): 3
'
'Insert into the wrapped List:
'Insert(2, "Oviraptor")
'
'Tyrannosaurus
'Amargasaurus
'Oviraptor
'Deinonychus
'Compsognathus
'
'Copied array has 7 elements:
'""
'"Tyrannosaurus"
'"Amargasaurus"
'"Oviraptor"
'"Deinonychus"
'"Compsognathus"
'""
注解
foreach
Visual C++ 中的 C# 语言 (for each
、 For Each
Visual Basic) 中的 C# 语言语句隐藏枚举器的复杂性。 因此,建议使用 foreach
,而不是直接操作枚举数。
枚举器可用于读取集合中的数据,但不能用于修改基础集合。
最初,枚举数定位在集合中第一个元素的前面。 在此位置上,未定义 Current。 因此,在读取 MoveNext 的值之前,必须调用 Current 将枚举器向前移动到集合的第一个元素。
在调用 Current 之前,MoveNext 返回相同的对象。 MoveNext 将 Current 设置为下一个元素。
如果 MoveNext 传递集合的末尾,则枚举器位于集合中最后一个元素之后,并 MoveNext 返回 false
。 当枚举器位于此位置时,对 MoveNext 的后续调用也会返回 false
。 如果最后一次MoveNext调用返回 ,false
Current则为未定义。 无法再次将 Current 设置为集合的第一个元素;必须改为创建新的枚举器实例。
只要集合保持不变,枚举器就仍有效。 如果对集合进行更改(如添加、修改或删除元素),则枚举数将失效且不可恢复,而且其行为是不确定的。
枚举数没有对集合的独占访问权;因此,从头到尾对一个集合进行枚举在本质上不是一个线程安全的过程。 若要确保枚举过程中的线程安全性,可以在整个枚举过程中锁定集合。 若要允许多个线程访问集合以进行读写操作,则必须实现自己的同步。
中 System.Collections.Generic 集合的默认实现不会同步。
此方法是 O (1) 操作。