元编程参考文档

字典例程

22 个节点

本页内容 22

添加

函数

Adds a new entry, throwing System.ArgumentException if the dictionary already has the key, so an accidental duplicate is caught rather than silently overwriting.

签名
添加(字典: TKey 到 TValue 的字典, 键: TKey, 值: TValue)
void Add(
    Dictionary<TKey, TValue> dictionary,
    TKey key,
    TValue value)

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to change; must not be null.
键TKey—The new key; a null key throws System.ArgumentNullException.
值TValue—The value to store.
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

添加,重复则拒绝

函数

Adds a new entry like DictionaryRoutines.TryAdd, and when the key already exists also gives you a message naming the duplicate key, ready to show or log; the existing entry is left untouched.

签名
添加,重复则拒绝 -> (结果: 真否, 错误: 文本)
    字典: TKey 到 TValue 的字典
    键: TKey
    值: TValue
bool AddOrRejectDuplicate(
    Dictionary<TKey, TValue> dictionary,
    TKey key,
    TValue value,
    out string error)

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to change; must not be null.
键TKey—The new key; a null key throws System.ArgumentNullException.
值TValue—The value to store.

返回

结果真否
错误文本
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

添加多项

函数

Adds many key/value pairs at once, in the order given.

签名
添加多项
    字典: TKey 到 TValue 的字典
    项: IEnumerable<KeyValuePair<TKey, TValue>>
    覆盖已有项: 真否
void AddRange(
    Dictionary<TKey, TValue> dictionary,
    IEnumerable<KeyValuePair<TKey, TValue>> items,
    bool overwriteExisting = false)

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to change; must not be null.
项IEnumerable<KeyValuePair<TKey, TValue>>—The pairs to add; must not be null.
覆盖已有项真否FalseWhen true, a key that already exists (or repeats within Items) takes the later value. When false, the first such key throws System.ArgumentException, and the pairs added before it stay in the dictionary.
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

容量

函数

How many entries the dictionary can hold before it next has to grow its storage; always at least its count.

签名
容量(字典: TKey 到 TValue 的字典) -> 整数
int Capacity(Dictionary<TKey, TValue> dictionary)

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to inspect; must not be null.

返回

结果整数
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

清空

函数

Removes every entry; the dictionary keeps its current capacity, so refilling it does not have to grow it again.

签名
清空(字典: TKey 到 TValue 的字典)
void Clear(Dictionary<TKey, TValue> dictionary)

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to empty; must not be null.
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

复制字典

函数

Makes a new dictionary holding the same entries as Source; adding or removing entries in one afterwards does not affect the other, but the values themselves are not copied, so a value of a reference type is shared by both.

签名
复制字典 -> TKey 到 TValue 的字典
    源: TKey 到 TValue 的字典
    比较器: IEqualityComparer<TKey>
Dictionary<TKey, TValue> Clone(
    Dictionary<TKey, TValue> source,
    IEqualityComparer<TKey> comparer)

参数

名称类型默认值说明
源TKey 到 TValue 的字典—The dictionary to copy; must not be null.
比较器IEqualityComparer<TKey>—How the copy compares keys; null means the key type's default equality, not the comparer Source uses. If this comparer treats two of the source's keys as the same, System.ArgumentException is thrown.

返回

结果TKey 到 TValue 的字典
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

比较器

函数

The comparer the dictionary uses to decide whether two keys are the same, so you can make another dictionary that matches keys the same way.

签名
比较器(字典: TKey 到 TValue 的字典) -> IEqualityComparer<TKey>
IEqualityComparer<TKey> Comparer(Dictionary<TKey, TValue> dictionary)

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to inspect; must not be null.

返回

结果IEqualityComparer<TKey>
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

包含键

函数

Whether the dictionary has an entry for Key, matched with the dictionary's key comparer.

签名
包含键(字典: TKey 到 TValue 的字典, 键: TKey) -> 真否
bool ContainsKey(Dictionary<TKey, TValue> dictionary, TKey key)

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to search; must not be null.
键TKey—The key to look for; a null key throws System.ArgumentNullException.

返回

结果真否
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

包含值

函数

Whether any entry has Value as its value, compared with the value type's default equality.

签名
包含值(字典: TKey 到 TValue 的字典, 值: TValue) -> 真否
bool ContainsValue(
    Dictionary<TKey, TValue> dictionary,
    TValue value)

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to search; must not be null.
值TValue—The value to look for; may be null.

返回

结果真否

备注

Unlike a key lookup this checks every entry in turn, so it gets slower as the dictionary grows.

所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

项数

函数

How many key/value pairs the dictionary holds.

签名
项数(字典: TKey 到 TValue 的字典) -> 整数
int Count(Dictionary<TKey, TValue> dictionary)

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to measure; must not be null.

返回

结果整数
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

创建字典

函数

重载 1/4

签名
创建字典() -> TKey 到 TValue 的字典
Dictionary<TKey, TValue> Create()

Makes a new, empty dictionary whose keys are compared with the key type's default equality.

返回

结果TKey 到 TValue 的字典

重载 2/4

签名
创建字典(容量: 整数) -> TKey 到 TValue 的字典
Dictionary<TKey, TValue> Create(int capacity)

Makes a new, empty dictionary with room reserved for at least Capacity entries, so it does not have to grow while you fill it; use it when you know roughly how many entries are coming.

参数

名称类型默认值说明
容量整数—How many entries to reserve room for; a negative number throws System.ArgumentOutOfRangeException.

返回

结果TKey 到 TValue 的字典

重载 3/4

签名
创建字典(比较器: IEqualityComparer<TKey>) -> TKey 到 TValue 的字典
Dictionary<TKey, TValue> Create(IEqualityComparer<TKey> comparer)

Makes a new, empty dictionary that uses Comparer to decide whether two keys are the same, for example StringComparer.OrdinalIgnoreCase for case-insensitive text keys.

参数

名称类型默认值说明
比较器IEqualityComparer<TKey>—How keys are compared; null means the key type's default equality.

返回

结果TKey 到 TValue 的字典

重载 4/4

签名
创建字典 -> TKey 到 TValue 的字典
    容量: 整数
    比较器: IEqualityComparer<TKey>
Dictionary<TKey, TValue> Create(
    int capacity,
    IEqualityComparer<TKey> comparer)

Makes a new, empty dictionary with room reserved for at least Capacity entries and keys compared by Comparer.

参数

名称类型默认值说明
容量整数—How many entries to reserve room for; a negative number throws System.ArgumentOutOfRangeException.
比较器IEqualityComparer<TKey>—How keys are compared; null means the key type's default equality.

返回

结果TKey 到 TValue 的字典
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

确保容量

函数

Grows the dictionary's storage, if needed, so it can hold at least Capacity entries without growing again; it never shrinks it.

签名
确保容量(字典: TKey 到 TValue 的字典, 容量: 整数) -> 整数
int EnsureCapacity(
    Dictionary<TKey, TValue> dictionary,
    int capacity)

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to change; must not be null.
容量整数—The number of entries to make room for; a negative number throws System.ArgumentOutOfRangeException.

返回

结果整数
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

获取值

函数

The value stored under Key; use it when the key must be there, and DictionaryRoutines.GetOrDefault or DictionaryRoutines.TryGet when it may not be.

签名
获取值(字典: TKey 到 TValue 的字典, 键: TKey) -> TValue
TValue Get(Dictionary<TKey, TValue> dictionary, TKey key)

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to read; must not be null.
键TKey—The key to look up; a missing key throws Generic.KeyNotFoundException and a null key throws System.ArgumentNullException.

返回

ResultTValue
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

获取值或默认值

函数

The value stored under Key, or Default Value when the dictionary has no such key.

签名
获取值或默认值 -> TValue
    字典: TKey 到 TValue 的字典
    键: TKey
    默认值: TValue
TValue GetOrDefault(
    Dictionary<TKey, TValue> dictionary,
    TKey key,
    TValue defaultValue)

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to read; must not be null.
键TKey—The key to look up; a null key throws System.ArgumentNullException.
默认值TValue—What to answer when the key is missing; when omitted, the value type's default (0, false or null).

返回

ResultTValue
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

所有键

函数

The dictionary's keys, in no guaranteed order.

签名
所有键(字典: TKey 到 TValue 的字典) -> Dictionary<TKey, TValue>.KeyCollection
Dictionary<TKey, TValue>.KeyCollection Keys(Dictionary<TKey, TValue> dictionary)

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to read; must not be null.

返回

结果Dictionary<TKey, TValue>.KeyCollection

备注

This is a live view rather than a copy: it reflects later additions and removals, and enumerating it while the dictionary is changed throws System.InvalidOperationException. Use DictionaryRoutines.ToArray for a snapshot.

所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

移除

函数

重载 1/2

签名
移除(字典: TKey 到 TValue 的字典, 键: TKey) -> 真否
bool Remove(Dictionary<TKey, TValue> dictionary, TKey key)

Removes the entry for Key, if there is one.

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to change; must not be null.
键TKey—The key to remove; a null key throws System.ArgumentNullException.

返回

结果真否

重载 2/2

签名
移除 -> (结果: 真否, 值: TValue)
    字典: TKey 到 TValue 的字典
    键: TKey
bool Remove(
    Dictionary<TKey, TValue> dictionary,
    TKey key,
    out TValue value)

Removes the entry for Key and hands back the value it held, so you can take an item out and use it in one step.

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to change; must not be null.
键TKey—The key to remove; a null key throws System.ArgumentNullException.

返回

结果真否
值TValue
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

设置值

函数

Stores Value under Key, adding a new entry or replacing the value already there; use DictionaryRoutines.Add instead when a duplicate key should be an error.

签名
设置值(字典: TKey 到 TValue 的字典, 键: TKey, 值: TValue)
void Set(
    Dictionary<TKey, TValue> dictionary,
    TKey key,
    TValue value)

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to change; must not be null.
键TKey—The key to store under; a null key throws System.ArgumentNullException.
值TValue—The value to store.
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

转为数组

函数

Copies every key/value pair into a new array, a snapshot that later changes to the dictionary do not affect.

签名
转为数组(字典: TKey 到 TValue 的字典) -> KeyValuePair<TKey, TValue> 数组
KeyValuePair<TKey, TValue>[] ToArray(Dictionary<TKey, TValue> dictionary)

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to copy; must not be null.

返回

结果KeyValuePair<TKey, TValue> 数组
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

释放多余容量

函数

重载 1/2

签名
释放多余容量(字典: TKey 到 TValue 的字典)
void TrimExcess(Dictionary<TKey, TValue> dictionary)

Shrinks the dictionary's storage to fit the entries it holds now, giving back memory after many removals.

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to change; must not be null.

重载 2/2

签名
释放多余容量(字典: TKey 到 TValue 的字典, 容量: 整数)
void TrimExcess(
    Dictionary<TKey, TValue> dictionary,
    int capacity)

Shrinks the dictionary's storage to fit Capacity entries, leaving room to grow to that size without reallocating.

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to change; must not be null.
容量整数—The number of entries to keep room for; less than the current count throws System.ArgumentOutOfRangeException.
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

尝试添加

函数

Adds a new entry only if the key is not already there; an existing entry is left untouched.

签名
尝试添加 -> 真否
    字典: TKey 到 TValue 的字典
    键: TKey
    值: TValue
bool TryAdd(
    Dictionary<TKey, TValue> dictionary,
    TKey key,
    TValue value)

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to change; must not be null.
键TKey—The new key; a null key throws System.ArgumentNullException.
值TValue—The value to store.

返回

结果真否
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

尝试获取值

函数

Looks up Key without failing when it is missing, telling you both whether it was found and what it holds.

签名
尝试获取值 -> (结果: 真否, 值: TValue)
    字典: TKey 到 TValue 的字典
    键: TKey
bool TryGet(
    Dictionary<TKey, TValue> dictionary,
    TKey key,
    out TValue value)

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to read; must not be null.
键TKey—The key to look up; a null key throws System.ArgumentNullException.

返回

结果真否
值TValue
所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库

所有值

函数

The dictionary's values, in the same order as DictionaryRoutines.Keys.

签名
所有值(字典: TKey 到 TValue 的字典) -> Dictionary<TKey, TValue>.ValueCollection
Dictionary<TKey, TValue>.ValueCollection Values(Dictionary<TKey, TValue> dictionary)

参数

名称类型默认值说明
字典TKey 到 TValue 的字典—The dictionary to read; must not be null.

返回

结果Dictionary<TKey, TValue>.ValueCollection

备注

This is a live view rather than a copy: it reflects later changes, and enumerating it while the dictionary is changed throws System.InvalidOperationException.

所属发行版
元编程(教育版)元编程(专业版)
可用的图种类
数据流 函数 事件 例程 蓝图 状态机状态
声明于
字典例程
所属包
标准库