ConcurrentDictionary<TKey,TValue>.TryRemove Método

Definição

Sobrecargas

TryRemove(TKey, TValue)

Tenta remover e retornar o valor com a chave especificada do ConcurrentDictionary<TKey,TValue>.

TryRemove(KeyValuePair<TKey,TValue>)

Remove uma chave e o valor do dicionário.

TryRemove(TKey, TValue)

Origem:
ConcurrentDictionary.cs
Origem:
ConcurrentDictionary.cs
Origem:
ConcurrentDictionary.cs

Tenta remover e retornar o valor com a chave especificada do ConcurrentDictionary<TKey,TValue>.

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

Parâmetros

key
TKey

A chave do elemento a ser removido e retornado.

value
TValue

Quando esse método retorna, contém o objeto removido do ConcurrentDictionary<TKey,TValue>ou o valor padrão do TValue tipo, se key não existir.

Retornos

true se um objeto tiver sido removido com êxito; caso contrário, false.

Exceções

key é null.

Exemplos

O exemplo a seguir mostra como chamar o ConcurrentDictionary<TKey,TValue>.TryRemove método :

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

Confira também

Aplica-se a

TryRemove(KeyValuePair<TKey,TValue>)

Origem:
ConcurrentDictionary.cs
Origem:
ConcurrentDictionary.cs
Origem:
ConcurrentDictionary.cs

Remove uma chave e o valor do dicionário.

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

Parâmetros

item
KeyValuePair<TKey,TValue>

O KeyValuePair<TKey,TValue> que representa a chave e o valor a serem removidos.

Retornos

true se a chave e o valor representados por item forem encontrados e removidos com êxito; caso contrário, false.

Exceções

A propriedade Key de item é null.

Comentários

A chave especificada e o valor devem corresponder à entrada no dicionário para que ela seja removida.

A chave é comparada usando o comparador do dicionário (ou o comparador padrão para TKey se nenhum comparador foi fornecido ao dicionário quando foi construído). O valor é comparado usando o comparador padrão para TValue.

Aplica-se a