ConcurrentDictionary<TKey,TValue>.TryRemove Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Overload
TryRemove(TKey, TValue) |
Tenta di rimuovere e restituire il valore con la chiave specificata da ConcurrentDictionary<TKey,TValue>. |
TryRemove(KeyValuePair<TKey,TValue>) |
Rimuove una chiave e un valore dal dizionario. |
TryRemove(TKey, TValue)
- Origine:
- ConcurrentDictionary.cs
- Origine:
- ConcurrentDictionary.cs
- Origine:
- ConcurrentDictionary.cs
Tenta di rimuovere e restituire il valore con la chiave specificata da ConcurrentDictionary<TKey,TValue>.
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
Parametri
- key
- TKey
Chiave dell'elemento da rimuovere e restituire.
- value
- TValue
Quando termina, questo metodo contiene l'oggetto rimosso da ConcurrentDictionary<TKey,TValue>o il valore predefinito del TValue
tipo se key
non esiste.
Restituisce
true
se l'oggetto è stato rimosso correttamente; in caso contrario, false
.
Eccezioni
key
è null
.
Esempio
Nell'esempio seguente viene illustrato come chiamare il ConcurrentDictionary<TKey,TValue>.TryRemove metodo :
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
Vedi anche
Si applica a
TryRemove(KeyValuePair<TKey,TValue>)
- Origine:
- ConcurrentDictionary.cs
- Origine:
- ConcurrentDictionary.cs
- Origine:
- ConcurrentDictionary.cs
Rimuove una chiave e un valore dal dizionario.
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
Parametri
KeyValuePair<TKey,TValue> che rappresenta la chiave e il valore da rimuovere.
Restituisce
true
se la chiave e il valore rappresentati da item
vengono trovati e rimossi; in caso contrario, false
.
Eccezioni
La proprietà Key di item
è null
.
Commenti
Sia la chiave specificata che il valore devono corrispondere alla voce nel dizionario affinché venga rimossa.
La chiave viene confrontata usando l'operatore di confronto del dizionario (o l'operatore di confronto predefinito per TKey
se non è stato fornito alcun operatore di confronto al dizionario al momento della costruzione). Il valore viene confrontato usando l'operatore di confronto predefinito per TValue
.