字典例程
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
值: TValuebool 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. |
| 覆盖已有项 | 真否 | False | When 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. |
返回
| Result | TValue |
- 所属发行版
- 元编程(教育版)元编程(专业版)
- 可用的图种类
- 数据流 函数 事件 例程 蓝图 状态机状态
- 声明于
- 字典例程
- 所属包
- 标准库
获取值或默认值
函数The value stored under Key, or Default Value when the dictionary has no such key.
获取值或默认值 -> TValue
字典: TKey 到 TValue 的字典
键: TKey
默认值: TValueTValue 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). |
返回
| Result | TValue |
- 所属发行版
- 元编程(教育版)元编程(专业版)
- 可用的图种类
- 数据流 函数 事件 例程 蓝图 状态机状态
- 声明于
- 字典例程
- 所属包
- 标准库
所有键
函数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 的字典
键: TKeybool 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
值: TValuebool 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 的字典
键: TKeybool 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.
- 所属发行版
- 元编程(教育版)元编程(专业版)
- 可用的图种类
- 数据流 函数 事件 例程 蓝图 状态机状态
- 声明于
- 字典例程
- 所属包
- 标准库