Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ConcurrentDictionary<TKey, TValue>::TryAdd Method (TKey, TValue)

.NET Framework (current version)
 

Attempts to add the specified key and value to the System.Collections.Concurrent::ConcurrentDictionary<TKey, TValue>.

Namespace:   System.Collections.Concurrent
Assembly:  mscorlib (in mscorlib.dll)

public:
bool TryAdd(
	TKey key,
	TValue value
)

Parameters

key
Type: TKey

The key of the element to add.

value
Type: TValue

The value of the element to add. The value can be null for reference types.

Return Value

Type: System::Boolean

true if the key/value pair was added to the System.Collections.Concurrent::ConcurrentDictionary<TKey, TValue> successfully; false if the key already exists.

Exception Condition
ArgumentNullException

key is null.

OverflowException

The dictionary already contains the maximum number of elements (MaxValue).

This method returns false if the key already exists. Use the TryUpdate or AddOrUpdate method to update the value in case a key already exists.

The following example shows how to call the ConcurrentDictionary<TKey, TValue>::TryAddmethod:

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone
Available since 8.1
Return to top
Show:
© 2017 Microsoft