Adds a key/value pair to the
IDictionary if the key does not already exist.
Syntax
Visual Basic (Declaration) | |
---|
Public Overloads Function GetOrAdd( _
ByVal key As TKey, _
ByVal value As TValue _
) As TValue |
Parameters
- key
- The key of the element to add.
- value
- The value to be added, if the key does not already exist.
Exceptions
Example
Library/Library.Test/TestSynchronizedCollections.cs
C# | Copy Code |
---|
SynchronizedDictionary<int, string> data =
new SynchronizedDictionary<int, string>(new Dictionary<int, string>());
Assert.AreEqual("a", data.GetOrAdd(1, "a"));
Assert.AreEqual("a", data.GetOrAdd(1, "b"));
Assert.AreEqual("b", data.GetOrAdd(2, k => "b"));
Assert.AreEqual("b", data.GetOrAdd(2, k => "c")); |
VB.NET | Copy Code |
---|
Dim data As New SynchronizedDictionary(Of Integer, String)(New Dictionary(Of Integer, String)())
Assert.AreEqual("a", data.GetOrAdd(1, "a"))
Assert.AreEqual("a", data.GetOrAdd(1, "b"))
Assert.AreEqual("b", data.GetOrAdd(2, Function(k) "b"))
Assert.AreEqual("b", data.GetOrAdd(2, Function(k) "c")) |
Requirements
Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7
See Also