ConcurrentDictionary<TKey,TValue>.TryRemove Method

Definition

Overloads

TryRemove(TKey, TValue)

Attempts to remove and return the value that has the specified key from the ConcurrentDictionary<TKey,TValue>.

TryRemove(KeyValuePair<TKey,TValue>)

Removes a key and value from the dictionary.

TryRemove(TKey, TValue)

Source:
ConcurrentDictionary.cs
Source:
ConcurrentDictionary.cs
Source:
ConcurrentDictionary.cs

Attempts to remove and return the value that has the specified key from the ConcurrentDictionary<TKey,TValue>.

C#
public bool TryRemove(TKey key, out TValue value);

Parameters

key
TKey

The key of the element to remove and return.

value
TValue

When this method returns, contains the object removed from the ConcurrentDictionary<TKey,TValue>, or the default value of the TValue type if key does not exist.

Returns

true if the object was removed successfully; otherwise, false.

Exceptions

key is null.

Examples

The following example shows how to call the ConcurrentDictionary<TKey,TValue>.TryRemove method:

C#
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!");
        }
}

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

TryRemove(KeyValuePair<TKey,TValue>)

Source:
ConcurrentDictionary.cs
Source:
ConcurrentDictionary.cs
Source:
ConcurrentDictionary.cs

Removes a key and value from the dictionary.

C#
public bool TryRemove(System.Collections.Generic.KeyValuePair<TKey,TValue> item);

Parameters

item
KeyValuePair<TKey,TValue>

The KeyValuePair<TKey,TValue> representing the key and value to remove.

Returns

true if the key and value represented by item are successfully found and removed; otherwise, false.

Exceptions

The Key property of item is null.

Remarks

Both the specifed key and value must match the entry in the dictionary for it to be removed.

The key is compared using the dictionary's comparer (or the default comparer for TKey if no comparer was provided to the dictionary when it was constructed). The value is compared using the default comparer for TValue.

Applies to

.NET 10 and other versions
Product Versions
.NET 5, 6, 7, 8, 9, 10