How can I enumerate through an IDictionary? Please refer to the code below.
public IDictionary<string, string> SelectDataSource
{
set
{
// This line does not work because it returns a generic enumerator,
// but mine is of type string,string
IDictionaryEnumerator enumerator = value.GetEnumerator();
}
}
Manual enumeration is very rare (compared to
foreach, for example) – the first thing I’d suggest is: check you really need that. However, since a dictionary enumerates as key-value-pair:should work. Or if it is only a method variable (not a field), then:
or better (since if it isn’t a field it probably needs local disposal):
or best (“KISS”):
However, you should also always dispose any existing value when replaced. Also, a set-only property is exceptionally rare. You really might want to check there isn’t a simpler API here… for example, a method taking the dictionary as a parameter.