views:

143

answers:

4

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

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

So, what do you think, just stick with Pop or is there a better term for "find and remove an element"?

(I feel kind of embarrassed to ask this question, it seems like such a trivial matter... but I like to use standards and I don't have wide experience with many languages/environments.)

EDIT: Sorry, should have explained more.... In this instance I can't use the term Remove since it's already defined by the class that I'm extending with a new method.

EDIT 2: Okay, here's what I have so far, inspired by the wisdom of the crowd as it were:

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;
}
+1  A: 

anything in particular wrong with the Remove(key) method already provided?

oh wait, you want to return the element removed also? how about

object valueRemoved = someDictionary.RemoveElement(key)

easily implemented as

if (!dict.ContainsKey(key))
{
    return null;
}
object val = dict[key];
dict.Remove(key);
return val;
Steven A. Lowe
Well, it doesn't return the element so it's always 2 lines of code (to retrieve first and then remove) and it's something I'm liable to do thousands of times across projects, so I figured it's not a bad idea for a standard extension method.
wizlb
Also, sorry if I changed the title before you answered, it previously said "find and remove", not "find, remove and return".
wizlb
RemoveElement, cool. That might work, although I might skip it if a shorter name comes along! Going to think about it while I get back to work and see what else pops up here.
wizlb
@wizlb: it's only 2 lines of code if you know the key exists ;-)
Steven A. Lowe
I know, but C# doesn't have expansion macros (and I'm really lazy). I guess I could use a snippet too...
wizlb
@wizlb: i think an extension method is a good idea, but be sure to check that the key exists and handle null results ;-)
Steven A. Lowe
+1  A: 

My first guess would have been "Take", but as you pointed out, that is already taken. My next suggestions would be "Extract" or "Detach", but they may be ambiguous.

How about "Withdraw" or "PullOut"?

Michael Stum
Actually, maybe I'll use TakeItem, adding Item after the method that I really want to use as Steven A. Lowe suggested above. So that's half credit for both you guys if I use it :) I'll just bump your answers instead of marking anyone as 'the answerer' for this question.
wizlb
ok, you're the original Extract guy and I like to give credit where it's due. But Mark's analogy really pulled it in for me. Regardless, everyone gets a point from me :)
wizlb
+6  A: 

I reckon Extract, like when archaeologists Find a mummy in the ground, Remove it and then Return it to a museum :)

Mark
You sir, have made a _very_ strong argument. I love the analogy. Also, this is what the military calls it when they go in to extract their troops.
wizlb
I like analogies, they make things so much simpler...
Mark
yeah, extract is better than RemoveElement
Steven A. Lowe
+1  A: 

This action is slightly reminiscent of an old collaboration platform (mainly academic) called Linda. In that environment, what you're talking about would be called "in". But that's a terrible name for it - they basically had the names backwards because they named them from the point of view of the shared tuple space. So, nevermind.

Extract is good, and "Pop" is also pretty obvious (just about anyone would know what you were doing, even though it's not a stack).

Ian Varley
Excellent. I'm always glad to get some input from someone who probably has a formal education in these matters as I'm completely self taught. I think I'll stick with Extract for now, since it also has a related method bool TryExtract(TKey key, out TValue value) and TryPop sounds wierd to me :)
wizlb