CollectionExtensions.TryAdd<TKey,TValue>(IDictionary<TKey,TValue>, TKey, TValue) 方法

定義

嘗試將指定的 keyvalue 新增至 dictionaryTries to add the specified key and value to the dictionary.

public:
generic <typename TKey, typename TValue>
[System::Runtime::CompilerServices::Extension]
 static bool TryAdd(System::Collections::Generic::IDictionary<TKey, TValue> ^ dictionary, TKey key, TValue value);
public static bool TryAdd<TKey,TValue> (this System.Collections.Generic.IDictionary<TKey,TValue> dictionary, TKey key, TValue value);
static member TryAdd : System.Collections.Generic.IDictionary<'Key, 'Value> * 'Key * 'Value -> bool
<Extension()>
Public Function TryAdd(Of TKey, TValue) (dictionary As IDictionary(Of TKey, TValue), key As TKey, value As TValue) As Boolean

類型參數

TKey

字典中的索引鍵類型。The type of the keys in the dictionary.

TValue

字典中的值類型。The type of the values in the dictionary.

參數

dictionary
IDictionary<TKey,TValue>

具有 TKey 類型索引鍵和 TValue 類型值的字典。A dictionary with keys of type TKey and values of type TValue.

key
TKey

要新增的值索引鍵。The key of the value to add.

value
TValue

要加入的值。The value to add.

傳回

Boolean

keyvalue 成功新增至 dictionary 時,為 true;當 dictionary 已包含指定的 key 時 (在此狀況下不新增任何項目),則為 falsetrue when the key and value are successfully added to the dictionary; false when the dictionary already contains the specified key, in which case nothing gets added.

例外狀況

dictionarynulldictionary is null.

適用於