CSharpTest.Net
TryAdd(TKey,TValue) Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Collections Namespace > LurchTable<TKey,TValue> Class > TryAdd Method : TryAdd(TKey,TValue) Method

key
The object to use as the key of the element to add.
value
The object to use as the value of the element to add.

Glossary Item Box

Adds an element with the provided key and value to the IDictionary.

Syntax

Visual Basic (Declaration) 
Public Overloads Function TryAdd( _
   ByVal key As TKey, _
   ByVal value As TValue _
) As Boolean
C# 
public bool TryAdd( 
   TKey key,
   TValue value
)

Parameters

key
The object to use as the key of the element to add.
value
The object to use as the value of the element to add.

Example

Library/Library.Test/TestLurchTable.cs

C#Copy Code
var data = new LurchTableTest<int, string>();

Assert.IsTrue(data.TryAdd(1, "a"));
Assert.IsFalse(data.TryAdd(1, "a"));

Assert.IsTrue(data.TryUpdate(1, "a"));
Assert.IsTrue(data.TryUpdate(1, "c"));
Assert.IsTrue(data.TryUpdate(1, "d", "c"));
Assert.IsFalse(data.TryUpdate(1, "f", "c"));
Assert.AreEqual("d", data[1]);
Assert.IsTrue(data.TryUpdate(1, "a", data[1]));
Assert.AreEqual("a", data[1]);
Assert.IsFalse(data.TryUpdate(2, "b"));

string val;
Assert.IsTrue(data.TryRemove(1, out val) && val == "a");
Assert.IsFalse(data.TryRemove(2, out val));
Assert.AreNotEqual(val, "a");

Assert.IsFalse(data.TryUpdate(1, (k, x) => x.ToUpper()));
data[1] = "a";
data[1] = "b";
Assert.IsTrue(data.TryUpdate(1, (k, x) => x.ToUpper()));
Assert.AreEqual("B", data[1]);
VB.NETCopy Code
Dim data As var = New LurchTableTest(Of Integer, String)()

Assert.IsTrue(data.TryAdd(1, "a"))
Assert.IsFalse(data.TryAdd(1, "a"))

Assert.IsTrue(data.TryUpdate(1, "a"))
Assert.IsTrue(data.TryUpdate(1, "c"))
Assert.IsTrue(data.TryUpdate(1, "d", "c"))
Assert.IsFalse(data.TryUpdate(1, "f", "c"))
Assert.AreEqual("d", data(1))
Assert.IsTrue(data.TryUpdate(1, "a", data(1)))
Assert.AreEqual("a", data(1))
Assert.IsFalse(data.TryUpdate(2, "b"))

Dim val As String
Assert.IsTrue(data.TryRemove(1, val) AndAlso val = "a")
Assert.IsFalse(data.TryRemove(2, val))
Assert.AreNotEqual(val, "a")

Assert.IsFalse(data.TryUpdate(1, Function(k, x) x.ToUpper()))
data(1) = "a"
data(1) = "b"
Assert.IsTrue(data.TryUpdate(1, Function(k, x) x.ToUpper()))
Assert.AreEqual("B", data(1))

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys