ConcurrentDictionary<TKey,TValue>.TryRemove 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
TryRemove(TKey, TValue) |
嘗試從 ConcurrentDictionary<TKey,TValue> 移除和傳回具有指定之索引鍵的值。 |
TryRemove(KeyValuePair<TKey,TValue>) |
從字典移除索引鍵和值。 |
TryRemove(TKey, TValue)
嘗試從 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
參數
- key
- TKey
要移除和傳回的項目索引鍵。
- value
- TValue
當這個方法傳回時,會包含從 ConcurrentDictionary<TKey,TValue>中移除的物件,如果key
不存在,TValue
則包含型別的預設值。
傳回
如果已成功移除物件,則為 true
,否則為 false
。
例外狀況
key
為 null
。
範例
下列範例示範如何呼叫 ConcurrentDictionary<TKey,TValue>.TryRemove 方法:
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
另請參閱
適用於
TryRemove(KeyValuePair<TKey,TValue>)
從字典移除索引鍵和值。
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
參數
KeyValuePair<TKey,TValue> 代表要移除的索引鍵及值。
傳回
若成功找到並移除 item
所表示的索引鍵及值,則為 true
,否則為 false
。
例外狀況
item
的 Key 屬性為 null
。
備註
指定的索引鍵和值必須符合字典中的專案,才能移除。
索引鍵是使用字典的比較子 (比較,或者,如果在建構) 時未提供比較子給字典,則為的預設比較子 TKey
。 此值會使用的預設比較子來 TValue
比較。