The term “find, delete, and return an item” in a set?

The name says mostly. I want to add a simple extension method to the Dictionary base class in C #. At first I was going to call it Pop (TKey key), like the Stack method, but it accepts the key to search for.

Then I was going to make Take (TKey key), but it matches the LINQ method with the same name ... and although C # 3.0 allows you to do this, I don't like it.

So, do you think you just stick to Pop or is there a better term for "find and delete an item"?

(I'm embarrassed to ask this question, it seems to be a trivial question ... but I like to use standards, and I don't have much experience working with many languages ​​and environments.)

EDIT: Sorry, I should have explained more .... In this case, I cannot use the term "Delete", because it is already defined by the class that I am distributing using the new method.

EDIT 2: Well, here is what I still inspired the crowd’s wisdom:

public static TValue Extract<TKey, TValue>
(
    this Dictionary<TKey, TValue> dict,
    TKey key
)
{
    TValue value = dict[key];
    dict.Remove(key);
    return value;
}

public static bool TryExtract<TKey, TValue>
(
    this Dictionary<TKey, TValue> dict,
    TKey key,
    out TValue value
)
{
    if( !dict.TryGetValue(key, out value) )
    {
        return false;
    }
    dict.Remove(key);
    return true;
}
+3
source share
4 answers

I consider extraction , for example, when archaeologists find the mummy in the ground, delete it, and then return it to the museum :)

+8
source

Is there something special wrong with the delete method (key) already provided?

oh wait, do you also want to return the item? What about

object valueRemoved = someDictionary.RemoveElement(key)

easily implemented as

if (!dict.ContainsKey(key))
{
    return null;
}
object val = dict[key];
dict.Remove(key);
return val;
+1
source

"Take", , , . : "" "", .

"Withdraw" "PullOut"?

+1

( ) Linda. , , "in". - , . , .

, "" ( , , ).

+1

Source: https://habr.com/ru/post/1699552/


All Articles