IReadOnlyDictionary<TKey,TValue> 接口

定义

表示键/值对的泛型只读集合。Represents a generic read-only collection of key/value pairs.

generic <typename TKey, typename TValue>
public interface class IReadOnlyDictionary : System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IReadOnlyCollection<System::Collections::Generic::KeyValuePair<TKey, TValue>>
public interface IReadOnlyDictionary<TKey,TValue> : System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyCollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>
type IReadOnlyDictionary<'Key, 'Value> = interface
    interface IReadOnlyCollection<KeyValuePair<'Key, 'Value>>
    interface seq<KeyValuePair<'Key, 'Value>>
    interface IEnumerable
Public Interface IReadOnlyDictionary(Of TKey, TValue)
Implements IEnumerable(Of KeyValuePair(Of TKey, TValue)), IReadOnlyCollection(Of KeyValuePair(Of TKey, TValue))

类型参数

TKey

只读字典中的键的类型。The type of keys in the read-only dictionary.

TValue

只读字典中的值的类型。The type of values in the read-only dictionary.

派生
实现

注解

每个元素是存储在 KeyValuePair<TKey,TValue> 对象中的键/值对。Each element is a key/value pair that is stored in a KeyValuePair<TKey,TValue> object.

每个对都必须有一个唯一键。Each pair must have a unique key. 实现可以改变是否允许您指定为 null 的键。Implementations can vary in whether they allow you to specify a key that is null. 该值可以 null,并且不必是唯一的。The value can be null and does not have to be unique. IReadOnlyDictionary<TKey,TValue> 接口允许枚举包含的键和值,但并不表示任何特定的排序顺序。The IReadOnlyDictionary<TKey,TValue> interface allows the contained keys and values to be enumerated, but it does not imply any particular sort order.

C# 语言的 foreach 语句(在 Visual Basic 中为 For Each,在 C++ 中为 for each)需要集合中每个元素的类型。The foreach statement of the C# language (For Each in Visual Basic, for each in C++) requires the type of each element in the collection. 由于 IReadOnlyDictionary<TKey,TValue> 接口的每个元素都是一个键/值对,因此元素类型既不是键类型,也不是值类型。Because each element of the IReadOnlyDictionary<TKey,TValue> interface is a key/value pair, the element type is not the type of the key or the type of the value. 相反,元素类型为 KeyValuePair<TKey,TValue>,如以下示例所示。Instead, the element type is KeyValuePair<TKey,TValue>, as the following example illustrates.

for each(KeyValuePair<int, String^> kvp in myDictionary)
{
    Console::WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
foreach (KeyValuePair<int, string> kvp in myDictionary)
{
    Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
For Each kvp As KeyValuePair(Of Integer, String) In myDictionary
    Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value)
Next kvp

foreach 语句是枚举器周围的包装器,它仅允许从集合读取,不允许写入集合。The foreach statement is a wrapper around the enumerator; it allows only reading from the collection, not writing to the collection.

属性

Count

获取集合中的元素数。Gets the number of elements in the collection.

(继承自 IReadOnlyCollection<T>)
Item[TKey]

获取在只读目录中有指定键的元素。Gets the element that has the specified key in the read-only dictionary.

Keys

获取包含只读字典中的键的可枚举集合。Gets an enumerable collection that contains the keys in the read-only dictionary.

Values

获取包含只读字典中的值的可枚举集合。Gets an enumerable collection that contains the values in the read-only dictionary.

方法

ContainsKey(TKey)

确定只读字典是否包含具有指定键的元素。Determines whether the read-only dictionary contains an element that has the specified key.

GetEnumerator()

返回循环访问集合的枚举器。Returns an enumerator that iterates through a collection.

(继承自 IEnumerable)
TryGetValue(TKey, TValue)

获取与指定的键关联的值。Gets the value that is associated with the specified key.

扩展方法

GetValueOrDefault<TKey,TValue>(IReadOnlyDictionary<TKey,TValue>, TKey)

尝试获取与 key 中的指定 dictionary 相关联的值。Tries to get the value associated with the specified key in the dictionary.

GetValueOrDefault<TKey,TValue>(IReadOnlyDictionary<TKey,TValue>, TKey, TValue)

尝试获取 dictionary 中与指定键相关联的值。Tries to get the value associated with the specified key in the dictionary.

CopyToDataTable<T>(IEnumerable<T>)

在给定其泛型参数 DataTableDataRow 的输入 IEnumerable<T> 对象的情况下,返回包含 T 对象副本的 DataRowReturns a DataTable that contains copies of the DataRow objects, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)

在给定其泛型参数 DataRowDataTable 的输入 IEnumerable<T> 对象的情况下,将 T 对象复制到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)

在给定其泛型参数 DataRowDataTable 的输入 IEnumerable<T> 对象的情况下,将 T 对象复制到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

Ancestors<T>(IEnumerable<T>)

返回元素集合,其中包含源集合中每个节点的上级。Returns a collection of elements that contains the ancestors of every node in the source collection.

Ancestors<T>(IEnumerable<T>, XName)

返回经过筛选的元素集合,其中包含源集合中每个节点的上级。Returns a filtered collection of elements that contains the ancestors of every node in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

DescendantNodes<T>(IEnumerable<T>)

返回源集合中每个文档和元素的子代节点的集合。Returns a collection of the descendant nodes of every document and element in the source collection.

Descendants<T>(IEnumerable<T>)

返回元素集合,其中包含源集合中每个元素和文档的子代元素。Returns a collection of elements that contains the descendant elements of every element and document in the source collection.

Descendants<T>(IEnumerable<T>, XName)

返回经过筛选的元素集合,其中包含源集合中每个元素和文档的子代元素。Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

Elements<T>(IEnumerable<T>)

返回源集合中每个元素和文档的子元素的集合。Returns a collection of the child elements of every element and document in the source collection.

Elements<T>(IEnumerable<T>, XName)

返回源集合中经过筛选的每个元素和文档的子元素集合。Returns a filtered collection of the child elements of every element and document in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

InDocumentOrder<T>(IEnumerable<T>)

返回节点集合(其中包含源集合中的所有节点),并按文档顺序排列。Returns a collection of nodes that contains all nodes in the source collection, sorted in document order.

Nodes<T>(IEnumerable<T>)

返回源集合中每个文档和元素的子节点集合。Returns a collection of the child nodes of every document and element in the source collection.

Remove<T>(IEnumerable<T>)

将源集合中的每个节点从其父节点中移除。Removes every node in the source collection from its parent node.

适用于