Testing for one element in the dictionary <k, v>
5 answers
The only way (with framework 2.0) is to iterate over it with foreach.
Or create a method that does this, for example:
public static T GetFirstElementOrDefault<T>(IEnumerable<T> values) { T value = default(T); foreach(T val in values) { value = val; break; } return value; } It works with all IEnumerable and in your case T is KeyValuePair
+5
Graviton is correct, but to be a little safer (in case there are more than one element), you can do this:
yourDictionary.First(); And to be more secure, you could do this (if the dictionary was also empty):
yourDictionary.FirstOrDefault(); +5