字典例程
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.
- 所屬發行版
- 元編程(教育版)元編程(專業版)
- 可用的圖種類
- 資料流 函數 事件 例程 藍圖 狀態機狀態
- 宣告於
- 字典例程
- 所屬包
- 標準庫