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;
}