CSharpTest.Net
TryGetValue Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.BPlusTree Assembly > CSharpTest.Net.Collections Namespace > BPlusTree<TKey,TValue> Class : TryGetValue Method

key
value

Glossary Item Box

Gets the value associated with the specified key.

Syntax

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

Parameters

key
value

Example

BPlusTree/BPlusTree.Test/BasicTests.cs

C#Copy Code
string test;
using (BPlusTree<int, string> data = Create(Options))
{
    data.Add(2, "v2");
    data.Add(1, "v1");

    int i = 0;
    for (; i < 8; i++)
        data.TryAdd(i, "v" + i);
    for (i = 16; i >= 8; i--)
        data.TryAdd(i, "v" + i);
    data.TryAdd(13, "v" + i);

    for (i = 0; i <= 16; i++)
    {
        if (!data.TryGetValue(i, out test))
            throw new ApplicationException();
        Assert.AreEqual("v" + i, test);
    }

    data.Remove(1);
    data.Remove(3);
    IEnumerator<KeyValuePair<int, string>> e = data.GetEnumerator();
    Assert.IsTrue(e.MoveNext());
    Assert.AreEqual(0, e.Current.Key);
    data.Add(1, "v1");
    Assert.IsTrue(e.MoveNext());
    data.Add(3, "v3");
    Assert.IsTrue(e.MoveNext());
    data.Remove(8);
    Assert.IsTrue(e.MoveNext());
    e.Dispose();
    data.Add(8, "v8");

    i = 0;
    foreach (KeyValuePair<int, string> pair in data)
        Assert.AreEqual(pair.Key, i++);

    for (i = 0; i <= 16; i++)
        Assert.IsTrue(data.Remove(i) && data.TryAdd(i, "v" + i));

    for (i = 6; i <= 12; i++)
        Assert.IsTrue(data.Remove(i));

    for (i = 6; i <= 12; i++)
    {
        Assert.IsFalse(data.TryGetValue(i, out test));
        Assert.IsNull(test);
    }

    for (i = 0; i <= 5; i++)
    {
        Assert.IsTrue(data.TryGetValue(i, out test));
        Assert.AreEqual("v" + i, test);
    }

    for (i = 13; i <= 16; i++)
    {
        Assert.IsTrue(data.TryGetValue(i, out test));
        Assert.AreEqual("v" + i, test);
    }

    for (i = 0; i <= 16; i++)
        Assert.AreEqual(i < 6 || i > 12, data.Remove(i));
}
VB.NETCopy Code
Dim test As String
Using data As BPlusTree(Of Integer, String) = Create(Options)
    data.Add(2, "v2")
    data.Add(1, "v1")

    Dim i As Integer = 0
    While i < 8
        data.TryAdd(i, "v" + i)
        System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
    End While
    i = 16
    While i >= 8
        data.TryAdd(i, "v" + i)
        System.Math.Max(System.Threading.Interlocked.Decrement(i),i + 1)
    End While
    data.TryAdd(13, "v" + i)

    i = 0
    While i <= 16
        If Not data.TryGetValue(i, test) Then
            Throw New ApplicationException()
        End If
        Assert.AreEqual("v" + i, test)
        System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
    End While

    data.Remove(1)
    data.Remove(3)
    Dim e As IEnumerator(Of KeyValuePair(Of Integer, String)) = data.GetEnumerator()
    Assert.IsTrue(e.MoveNext())
    Assert.AreEqual(0, e.Current.Key)
    data.Add(1, "v1")
    Assert.IsTrue(e.MoveNext())
    data.Add(3, "v3")
    Assert.IsTrue(e.MoveNext())
    data.Remove(8)
    Assert.IsTrue(e.MoveNext())
    e.Dispose()
    data.Add(8, "v8")

    i = 0
    For Each pair As KeyValuePair(Of Integer, String) In data
        Assert.AreEqual(pair.Key, System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1))
    Next

    i = 0
    While i <= 16
        Assert.IsTrue(data.Remove(i) AndAlso data.TryAdd(i, "v" + i))
        System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
    End While

    i = 6
    While i <= 12
        Assert.IsTrue(data.Remove(i))
        System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
    End While

    i = 6
    While i <= 12
        Assert.IsFalse(data.TryGetValue(i, test))
        Assert.IsNull(test)
        System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
    End While

    i = 0
    While i <= 5
        Assert.IsTrue(data.TryGetValue(i, test))
        Assert.AreEqual("v" + i, test)
        System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
    End While

    i = 13
    While i <= 16
        Assert.IsTrue(data.TryGetValue(i, test))
        Assert.AreEqual("v" + i, test)
        System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
    End While

    i = 0
    While i <= 16
        Assert.AreEqual(i < 6 OrElse i > 12, data.Remove(i))
        System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
    End While
End Using

Requirements

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

See Also

Generated with Document! X 2011 by Innovasys