ConcurrentDictionary<TKey,TValue>.TryUpdate 方法

定义

如果具有 key 的现有值等于 comparisonValue,则将与 key 关联的值更新为 newValue

public:
 bool TryUpdate(TKey key, TValue newValue, TValue comparisonValue);
public bool TryUpdate (TKey key, TValue newValue, TValue comparisonValue);
member this.TryUpdate : 'Key * 'Value * 'Value -> bool
Public Function TryUpdate (key As TKey, newValue As TValue, comparisonValue As TValue) As Boolean

参数

key
TKey

comparisonValue 进行比较并且可能被替换的值键。

newValue
TValue

一个值,当比较结果相等时,将替换具有指定 key 的元素的值。

comparisonValue
TValue

与具有指定 key 的元素的值进行比较的值。

返回

Boolean

如果具有 true 的值与 key 相等且被替换为 comparisonValue,则为 newValue;否则为 false

例外

keynull

示例

以下示例演示如何调用 TryUpdate 该方法:

class CD_TryXYZ
{
        // Demonstrates:
        //      ConcurrentDictionary<TKey, TValue>.TryAdd()
        //      ConcurrentDictionary<TKey, TValue>.TryUpdate()
        //      ConcurrentDictionary<TKey, TValue>.TryRemove()
        static void Main()
        {
            int numFailures = 0; // for bookkeeping

            // Construct an empty dictionary
            ConcurrentDictionary<int, String> cd = new ConcurrentDictionary<int, string>();

            // This should work
            if (!cd.TryAdd(1, "one"))
            {
                Console.WriteLine("CD.TryAdd() failed when it should have succeeded");
                numFailures++;
            }

            // This shouldn't work -- key 1 is already in use
            if (cd.TryAdd(1, "uno"))
            {
                Console.WriteLine("CD.TryAdd() succeeded when it should have failed");
                numFailures++;
            }

            // Now change the value for key 1 from "one" to "uno" -- should work
            if (!cd.TryUpdate(1, "uno", "one"))
            {
                Console.WriteLine("CD.TryUpdate() failed when it should have succeeded");
                numFailures++;
            }

            // Try to change the value for key 1 from "eine" to "one"
            //    -- this shouldn't work, because the current value isn't "eine"
            if (cd.TryUpdate(1, "one", "eine"))
            {
                Console.WriteLine("CD.TryUpdate() succeeded when it should have failed");
                numFailures++;
            }

            // Remove key/value for key 1.  Should work.
            string value1;
            if (!cd.TryRemove(1, out value1))
            {
                Console.WriteLine("CD.TryRemove() failed when it should have succeeded");
                numFailures++;
            }

            // Remove key/value for key 1.  Shouldn't work, because I already removed it
            string value2;
            if (cd.TryRemove(1, out value2))
            {
                Console.WriteLine("CD.TryRemove() succeeded when it should have failed");
                numFailures++;
            }

            // If nothing went wrong, say so
            if (numFailures == 0) Console.WriteLine("  OK!");
        }
}
'Imports System.Collections.Concurrent

Class CD_TryXYZ

    ' Demonstrates:
    ' ConcurrentDictionary<TKey, TValue>.TryAdd()
    ' ConcurrentDictionary<TKey, TValue>.TryUpdate()
    ' ConcurrentDictionary<TKey, TValue>.TryRemove()
    Shared Sub Main()
        Dim numFailures As Integer = 0
        ' for bookkeeping
        ' Construct an empty dictionary
        Dim cd As ConcurrentDictionary(Of Integer, [String]) = New ConcurrentDictionary(Of Integer, String)()

        ' This should work
        If Not cd.TryAdd(1, "one") Then
            Console.WriteLine("CD.TryAdd() failed when it should have succeeded")
            numFailures += 1
        End If

        ' This shouldn't work -- key 1 is already in use
        If cd.TryAdd(1, "uno") Then
            Console.WriteLine("CD.TryAdd() succeeded when it should have failed")
            numFailures += 1
        End If

        ' Now change the value for key 1 from "one" to "uno" -- should work
        If Not cd.TryUpdate(1, "uno", "one") Then
            Console.WriteLine("CD.TryUpdate() failed when it should have succeeded")
            numFailures += 1
        End If

        ' Try to change the value for key 1 from "eine" to "one" 
        ' -- this shouldn't work, because the current value isn't "eine"
        If cd.TryUpdate(1, "one", "eine") Then
            Console.WriteLine("CD.TryUpdate() succeeded when it should have failed")
            numFailures += 1
        End If

        ' Remove key/value for key 1. Should work.
        Dim value1 As String = ""
        If Not cd.TryRemove(1, value1) Then
            Console.WriteLine("CD.TryRemove() failed when it should have succeeded")
            numFailures += 1
        End If

        ' Remove key/value for key 1. Shouldn't work, because I already removed it
        Dim value2 As String = ""
        If cd.TryRemove(1, value2) Then
            Console.WriteLine("CD.TryRemove() succeeded when it should have failed")
            numFailures += 1
        End If

        ' If nothing went wrong, say so
        If numFailures = 0 Then
            Console.WriteLine(" OK!")
        End If
    End Sub
End Class

适用于

另请参阅