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,其值类型为 TValueA 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.

返回

如果 keyvalue 成功添加到 dictionary,则为 true;如果 dictionary 已包含指定的 key,则为 false,此情况下不会添加任何内容。true 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.

适用于