tags:

views:

72

answers:

1

How can I retrieve the value of the only item I have an a Dictionary (C#) in one line? Is this possible?

+10  A: 
dictionary.Single().Value

Single throws an exception if the dictionary doesn't contain exactly one element (if you don't like this behavior, use First() instead). If you're sure you're going to put a single item in a dictionary, why aren't you simply using a variable?

Mehrdad Afshari
Just for a test case. Is First() an extension method?
Greg
@Greg: Yes, it's an extension method declared in `System.Linq.Enumerable` class.
Mehrdad Afshari