I have a method with a signature
public void Foo(IDictionary<string, IEnumerable<string>> data)
{
}
and want to go through
private Dictionary<string, HashSet<string>> myInput =
new Dictionary<string, HashSet<string>>()
{
};
Line
Foo(myInput);
gives a compiler error:
Argument 1: cannot convert from System.Collections.Generic.Dictionary<string,System.Collections.Generic.HashSet<string>>toSystem.Collections.Generic.IDictionary<string,System.Collections.Generic.IEnumerable<string>>
Dictionary<K,V>implements IDictionary<K,V>and HashSet<T>implements IEnumerable<T>.
- Why can't the compiler perform the conversion?
- How to create an instance of data that I can transfer to
Foo?
Note
If I changed my signature to
public void Foo(IDictionary<string, HashSet<string>> data)
compilation succeeds. However, I do not need any knowledge that a particular type, for example HashSet<T>, is passed. Anyone IEnumerable<T>will do.
UPDATE
The following compilations:
public void Foo(IDictionary<string, IEnumerable<string>> data)
{
List<string> item = new List<string>() { "foo", "bar", "baz" };
data.Add("key", item);
HashSet<string> item2 = new HashSet<string>() { "quu" };
data.Add("key2", item2);
}
so clearly datacan take mixed-type values ββthat all implementIEnumerable<T>