ConcurrentDictionary<TKey,TValue>.TryRemove Methode

Definitie

Overloads

Name Description
TryRemove(TKey, TValue)

Hiermee wordt geprobeerd de waarde met de opgegeven sleutel uit de ConcurrentDictionary<TKey,TValue>sleutel te verwijderen en te retourneren.

TryRemove(KeyValuePair<TKey,TValue>)

Hiermee verwijdert u een sleutel en waarde uit de woordenlijst.

TryRemove(TKey, TValue)

Bron:
ConcurrentDictionary.cs
Bron:
ConcurrentDictionary.cs
Bron:
ConcurrentDictionary.cs
Bron:
ConcurrentDictionary.cs
Bron:
ConcurrentDictionary.cs

Hiermee wordt geprobeerd de waarde met de opgegeven sleutel uit de ConcurrentDictionary<TKey,TValue>sleutel te verwijderen en te retourneren.

public:
 bool TryRemove(TKey key, [Runtime::InteropServices::Out] TValue % value);
public bool TryRemove(TKey key, out TValue value);
member this.TryRemove : 'Key * 'Value -> bool
Public Function TryRemove (key As TKey, ByRef value As TValue) As Boolean

Parameters

key
TKey

De sleutel van het element dat moet worden verwijderd en geretourneerd.

value
TValue

Wanneer deze methode wordt geretourneerd, bevat het object dat is verwijderd uit het ConcurrentDictionary<TKey,TValue>type of de standaardwaarde van het TValue type als key deze niet bestaat.

Retouren

true als het object is verwijderd; anders, false.

Uitzonderingen

key is null.

Voorbeelden

In het volgende voorbeeld ziet u hoe u de ConcurrentDictionary<TKey,TValue>.TryRemove methode aanroept:

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!");
        }
}
// Demonstrates:
//      ConcurrentDictionary<TKey, TValue>.TryAdd()
//      ConcurrentDictionary<TKey, TValue>.TryUpdate()
//      ConcurrentDictionary<TKey, TValue>.TryRemove()

let mutable numFailures = 0 // for bookkeeping

// Construct an empty dictionary
let cd = ConcurrentDictionary<int, string>()

// This should work
if cd.TryAdd(1, "one") |> not then
    printfn "CD.TryAdd() failed when it should have succeeded"
    numFailures <- numFailures + 1

// This shouldn't work -- key 1 is already in use
if cd.TryAdd(1, "uno") then
    printfn "CD.TryAdd() succeeded when it should have failed"
    numFailures <- numFailures + 1

// Now change the value for key 1 from "one" to "uno" -- should work
if cd.TryUpdate(1, "uno", "one") |> not then
    printfn "CD.TryUpdate() failed when it should have succeeded"
    numFailures <- numFailures + 1

// 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
    printfn "CD.TryUpdate() succeeded when it should have failed"
    numFailures <- numFailures + 1

// Remove key/value for key 1.  Should work.
let mutable value1 = ""

if cd.TryRemove(1, &value1) |> not then
    printfn "CD.TryRemove() failed when it should have succeeded"
    numFailures <- numFailures + 1

// Remove key/value for key 1.  Shouldn't work, because I already removed it
let mutable value2 = ""

if cd.TryRemove(1, &value2) then
    printfn "CD.TryRemove() succeeded when it should have failed"
    numFailures <- numFailures + 1

// If nothing went wrong, say so
if numFailures = 0 then
    printfn "  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

Zie ook

Van toepassing op

TryRemove(KeyValuePair<TKey,TValue>)

Bron:
ConcurrentDictionary.cs
Bron:
ConcurrentDictionary.cs
Bron:
ConcurrentDictionary.cs
Bron:
ConcurrentDictionary.cs
Bron:
ConcurrentDictionary.cs

Hiermee verwijdert u een sleutel en waarde uit de woordenlijst.

public:
 bool TryRemove(System::Collections::Generic::KeyValuePair<TKey, TValue> item);
public bool TryRemove(System.Collections.Generic.KeyValuePair<TKey,TValue> item);
member this.TryRemove : System.Collections.Generic.KeyValuePair<'Key, 'Value> -> bool
Public Function TryRemove (item As KeyValuePair(Of TKey, TValue)) As Boolean

Parameters

item
KeyValuePair<TKey,TValue>

Het KeyValuePair<TKey,TValue> vertegenwoordigen van de sleutel en de waarde die u wilt verwijderen.

Retouren

trueals de sleutel en waarde die worden vertegenwoordigd dooritem, zijn gevonden en verwijderd; anders. false

Uitzonderingen

De Key eigenschap van item is null.

Opmerkingen

Zowel de opgegeven sleutel als de waarde moeten overeenkomen met de vermelding in de woordenlijst om deze te kunnen verwijderen.

De sleutel wordt vergeleken met behulp van de vergelijkingsfunctie van de woordenlijst (of de standaard-vergelijkingsfunctie voor TKey als er geen vergelijkingsfunctie is opgegeven voor de woordenlijst toen deze werd samengesteld). De waarde wordt vergeleken met behulp van de standaard comparer voor TValue.

Van toepassing op