ConcurrentDictionary<TKey,TValue>.TryRemove メソッド

定義

オーバーロード

TryRemove(TKey, TValue)

ConcurrentDictionary<TKey,TValue> から、指定したキーを持つ値を削除して返そうと試みます。

TryRemove(KeyValuePair<TKey,TValue>)

ディクショナリからキーと値を削除します。

TryRemove(TKey, TValue)

ソース:
ConcurrentDictionary.cs
ソース:
ConcurrentDictionary.cs
ソース:
ConcurrentDictionary.cs

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

例外

keynullです。

次の例は、 メソッドを呼び出す方法を 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>)

ソース:
ConcurrentDictionary.cs
ソース:
ConcurrentDictionary.cs
ソース:
ConcurrentDictionary.cs

ディクショナリからキーと値を削除します。

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

パラメーター

item
KeyValuePair<TKey,TValue>

削除するキーと値を表す KeyValuePair<TKey,TValue>

戻り値

item によって表されるキーと値が見つかり、正常に削除された場合は true。それ以外の場合は false

例外

itemKey プロパティが null です。

注釈

指定されたキーと値の両方が、ディクショナリ内のエントリと一致して削除される必要があります。

キーは、ディクショナリの比較子 (または、構築時にディクショナリに比較子が指定されなかった場合の既定 TKey の比較子) を使用して比較されます。 値は、 の既定の比較子 TValueを使用して比較されます。

適用対象