public static bool TryAdd<TKey, TValue>(
this IDictionary<TKey, TValue> dictionary,
KeyValuePair<TKey, TValue> item
)
<ExtensionAttribute>
Public Shared Function TryAdd(Of TKey, TValue) (
dictionary As IDictionary(Of TKey, TValue),
item As KeyValuePair(Of TKey, TValue)
) As Boolean
public:
[ExtensionAttribute]
generic<typename TKey, typename TValue>
static bool TryAdd(
IDictionary<TKey, TValue>^ dictionary,
KeyValuePair<TKey, TValue> item
)
[<ExtensionAttribute>]
static member TryAdd :
dictionary : IDictionary<'TKey, 'TValue> *
item : KeyValuePair<'TKey, 'TValue> -> bool
The CollectionExtensions class in .NET Core 2.0 and above also has a TryAdd method that behaves somewhat differently. To avoid ambiguity with that method this one has a KeyValuePairTKey, TValue parameter.
Unlike the CollectionExtensions.TryAdd method, this one is thread safe when used with ConcurrentDictionaryTKey, TValue, ThreadSafeDictionaryTKey, TValue and LockingDictionaryTKey, TValue instances. Additionally, this one returns if dictionary is read-only instead of throwing an exception.
ArgumentNullException | dictionary is . |
ArgumentException | item has a key. |