I'm using VS 2005 fx2.0.
If I know my Dictionary contains only 1 item how do I get to it?
Thanks, rod
The only way (with framework 2.0) is to iterate over it with foreach.
Or make a method that do that, like:
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
Make sure you have using System.Linq;
. The below command will get the key value pair of the dictionary
var item = Dictionary<k,v>.Single();
var key = item.Key;
var value =item.Value;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With