views:

107

answers:

5

In the case where Dictionary<object, object> myDictionary happens to contain a single item, what is the best way to retrieve the value object (if I don't care about the key) ?

if (myDictionary.Count == 1)
{
    // Doesn't work
    object obj = myDictionary.Values[0];
}

Thanks

+3  A: 
object obj = myDictionary.Values.Single();
mquander
+8  A: 

Depending on wether you want it to fail if there's multiple object or not you can use either

myDictionary.Value.Single();//Will fail if there's more than one

or

myDictionary.Value.First();//Will just return the first regardless of the count
Rune FS
.FirstOrDefault() is the safest, since it returns null if there are none, and the null can be checked safely.
Cylon Cat
@Cylon: However, that can't determine if it's null because the value of the item is null or because there was no item.
Guffa
@Gaffa, you can make that distinction, if it's important, by checking the number of keys in the dictionary. On the other hand, I'd consider that creating a key, with a corresponding null value, is a bug.
Cylon Cat
@cylon if no objects is an exceptional situation it should be treated as such, not masked as a null reference. returning null if your not testing for it is not safe and if you're going to test for null you might as well let the code show that no objects is an exceptional situation and then handle the potential exception
Rune FS
@Rune, "null" has a very specific meaning: there might be a value that could go here, but we don't know what it is. This is entirely different from "an exceptional condition" which should simply be thrown as an exception.
Cylon Cat
@cylon agreed null has by definition the value of unknown. That however doesn't make it safe (try calling a method)so it's not a matter of what's "safest" but wether or not the dictionary having a count != 1 is an exceptional situation or not
Rune FS
+2  A: 

I would never code for the assumption that there will only be one. If you know there's always going to be exactly one, then why use a dictionary?

Cylon Cat
A: 

I think you can use an iterator.

myDictionary.GetEnumerator().Current

hehewaffles
The enumerator starts before the first item, you have to call MoveNext to advance to the first item.
Guffa
+1  A: 

You can't get the value directly or by index, you have to either know the key:

object obj = yourDictionary[theKeyThatYouHappenToKnow];

or use an enumerator:

var en = yourDictionary.GetEnumerator();
en.MoveNext();
object obj = en.Current.Value;
en.Dispose();

If you are using framework 3.5, you can also some extension method like Single or First to use the enumerator for you.

Guffa