ReadOnlyCollection<T> 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为泛型只读集合提供基类。
generic <typename T>
public ref class ReadOnlyCollection : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IList<T>, System::Collections::Generic::IReadOnlyCollection<T>, System::Collections::Generic::IReadOnlyList<T>, System::Collections::IList
generic <typename T>
public ref class ReadOnlyCollection : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IList<T>, System::Collections::IList
public class ReadOnlyCollection<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IList<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.Generic.IReadOnlyList<T>, System.Collections.IList
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class ReadOnlyCollection<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IList<T>, System.Collections.IList
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class ReadOnlyCollection<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IList<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.Generic.IReadOnlyList<T>, System.Collections.IList
type ReadOnlyCollection<'T> = class
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface IList<'T>
interface IReadOnlyCollection<'T>
interface IReadOnlyList<'T>
interface ICollection
interface IList
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type ReadOnlyCollection<'T> = class
interface IList<'T>
interface ICollection<'T>
interface seq<'T>
interface IList
interface ICollection
interface IEnumerable
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type ReadOnlyCollection<'T> = class
interface IList<'T>
interface ICollection<'T>
interface IList
interface ICollection
interface IReadOnlyList<'T>
interface IReadOnlyCollection<'T>
interface seq<'T>
interface IEnumerable
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type ReadOnlyCollection<'T> = class
interface IList<'T>
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface IList
interface ICollection
interface IReadOnlyList<'T>
interface IReadOnlyCollection<'T>
type ReadOnlyCollection<'T> = class
interface IList<'T>
interface ICollection<'T>
interface IReadOnlyList<'T>
interface IReadOnlyCollection<'T>
interface seq<'T>
interface IList
interface ICollection
interface IEnumerable
Public Class ReadOnlyCollection(Of T)
Implements ICollection(Of T), IEnumerable(Of T), IList, IList(Of T), IReadOnlyCollection(Of T), IReadOnlyList(Of T)
Public Class ReadOnlyCollection(Of T)
Implements ICollection(Of T), IEnumerable(Of T), IList, IList(Of T)
类型参数
- T
集合中元素的类型。
- 继承
-
ReadOnlyCollection<T>
- 派生
- 属性
- 实现
示例
下面的代码示例演示 ReadOnlyCollection<T> 类的多个成员。 该代码示例创建字符串 List<T>,并向其中添加四个恐龙名称。 然后,代码示例将列表包装在 ReadOnlyCollection<T>中。
在演示 Count、Contains、Item[]和 IList.IndexOf 成员后,代码示例显示,ReadOnlyCollection<T> 只是原始 List<T> 的包装器,方法是向 List<T> 添加新项并显示 ReadOnlyCollection<T>的内容。
最后,代码示例创建一个大于集合的数组,并使用 CopyTo 方法将集合的元素插入数组中间。
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"
'""
注解
ReadOnlyCollection<T> 泛型类的实例始终为只读。 只读的集合只是一个集合,其中包含阻止修改集合的包装器;因此,如果对基础集合进行了更改,只读集合将反映这些更改。 有关此类的可修改版本,请参阅 Collection<T>。
继承者说明
提供了此基类,以便实现者更轻松地创建通用只读自定义集合。 鼓励实现者扩展此基类,而不是创建自己的基类。
构造函数
ReadOnlyCollection<T>(IList<T>) |
初始化 ReadOnlyCollection<T> 类的新实例,该实例是指定列表周围的只读包装器。 |
属性
Count |
获取 ReadOnlyCollection<T> 实例中包含的元素数。 |
Empty | |
Item[Int32] |
获取指定索引处的元素。 |
Items |
返回 ReadOnlyCollection<T> 包装的 IList<T>。 |
方法
Contains(T) |
确定元素是否在 ReadOnlyCollection<T>中。 |
CopyTo(T[], Int32) |
从目标数组的指定索引处开始,将整个 ReadOnlyCollection<T> 复制到兼容的一维 Array。 |
Equals(Object) |
确定指定的对象是否等于当前对象。 (继承自 Object) |
GetEnumerator() |
返回循环访问 ReadOnlyCollection<T>的枚举数。 |
GetHashCode() |
用作默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
IndexOf(T) |
搜索指定的对象,并返回整个 ReadOnlyCollection<T>中第一个匹配项的从零开始的索引。 |
MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
ToString() |
返回一个表示当前对象的字符串。 (继承自 Object) |
显式接口实现
扩展方法
适用于
线程安全性
此类型的公共静态(Shared
)成员是线程安全的。 不保证任何实例成员都是线程安全的。
只要集合未修改,ReadOnlyCollection<T> 就可以同时支持多个读取器。 即便如此,通过集合进行枚举本质上不是线程安全的过程。 若要保证枚举期间的线程安全性,可以在整个枚举期间锁定集合。 若要允许多个线程访问集合进行读取和写入,必须实现自己的同步。