【发布时间】:2011-08-27 03:27:23
【问题描述】:
请考虑以下代码:
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);
...
}
有错误:
错误 CS1503:参数“1”:不能 转换自 'System.Collections.Generic.Dictionary>' 到 'System.Collections.Generic.IDictionary>'
有没有办法调用 foo 函数?
【问题讨论】:
-
您实现了自己的集合?如果没有,看不出有任何理由避免使用
static void foo(Dictionary<StudentType, List<Student>> students)来解决这个问题。 -
static void foo(IDictionary<StudentType, IList<Student>> students)只是为了简单。 IDictionary 不是foo(),而是传递给我的应用程序的另一部分,因此有必要使用IDictionary<>和IList<>来提供抽象。
标签: c# list dictionary ilist idictionary