How can I retrieve the value of the only item I have an a Dictionary (C#) in one line? Is this possible?
views:
72answers:
1
+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
2010-05-13 07:26:05
Just for a test case. Is First() an extension method?
Greg
2010-05-13 08:42:02
@Greg: Yes, it's an extension method declared in `System.Linq.Enumerable` class.
Mehrdad Afshari
2010-05-13 11:19:00