In a general C # class for an internal reusable library, I would like to pass a link to "something that maps to other things." The data types of what is passed into them should not be known to the library. In addition, it should not be known how they are stored, that is, what is today a list stored in memory can later be a database table, which is read on demand.
So, I thought I would write this library class:
class GenericClass<T, U> { public void Foo(IDictionary<T, IEnumerable<U>> bar) { // do something } }
This compiles, but trying to pass specific implementations does not:
class UsingClass { public static void Main(string[] args) { var c = new GenericClass<string, string>(); c.Foo(new Dictionary<string, List<string>>()); } }
I get the following two syntax errors:
Filename.cs(46,13): error CS1502: The best overloaded method match for 'GenericClass<string,string>.Foo(System.Collections.Generic.IDictionary<string,System.Collections.Generic.IEnumerable<string>>)' has some invalid arguments Filename.cs(46,19): error CS1503: Argument 1: cannot convert from 'System.Collections.Generic.Dictionary<string,System.Collections.Generic.List<string>>' to 'System.Collections.Generic.IDictionary<string,System.Collections.Generic.IEnumerable<string>>'
Replacing IEnumerable with a Foo() declaration on List fixes it, but this, of course, is not quite what I want.
Is this really not supported by C # (4.0), or am I just missing something obvious? What workaround would you suggest? (I'm sure they talked a lot about this, so the links to great descriptions are great too.)
Yes, I should be able to write my own helper classes for myself, but why should I?