Dictionary<TKey,TValue>.TryAdd(TKey, TValue) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Attempts to add the specified key and value to the dictionary.
public:
bool TryAdd(TKey key, TValue value);
public bool TryAdd(TKey key, TValue value);
member this.TryAdd : 'Key * 'Value -> bool
Public Function TryAdd (key As TKey, value As TValue) As Boolean
- key
- TKey
The key of the element to add.
- value
- TValue
The value of the element to add. It can be null
.
true
if the key/value pair was added to the dictionary successfully; otherwise, false
.
key
is null
.
Unlike the Add method, this method doesn't throw an exception if the element with the given key exists in the dictionary. Unlike the Dictionary indexer, TryAdd
doesn't override the element if the element with the given key exists in the dictionary. If the key already exists, TryAdd
does nothing and returns false
.
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Standard | 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: