請考慮下面的代碼:<往前往IDictionary <StudentType,IList <Student>>?>學生類型列表<Student>>?
class Student
{
}
enum StudentType
{
}
static void foo(IDictionary<StudentType, IList<Student>> students)
{
}
static void Main(string[] args)
{
Dictionary<StudentType, List<Student>> studentDict =
new Dictionary<StudentType, List<Student>>();
foo(studentDict);
...
}
有錯誤:
error CS1503: Argument '1': cannot convert from 'System.Collections.Generic.Dictionary>' to 'System.Collections.Generic.IDictionary>'
有什麼辦法來調用foo的功能?
你實現自己的藏品?如果沒有,看不到有什麼理由避免使用'static void foo(Dictionary> students)'來解決這個問題。 –
2011-05-17 10:48:09
'static void foo(IDictionary> students) '只是爲了簡單。代替'foo()'IDictionary <>傳遞給我的應用程序的另一部分,所以有必要使用'IDictionary <>'和'IList <>'來提供一個抽象。 –
2011-05-17 10:55:22