如何将 Ilist 转换为 ArrayList?

Mas*_*ani 5 c# asp.net casting

我可以投IList进去ArrayList吗?

如果是,我该怎么办?

IList alls = RetrieveCourseStudents(cf);
ArrayList a = (ArrayList)alls;
Run Code Online (Sandbox Code Playgroud)

那是对的吗?

有错误:

无法转换类型的对象

Ger*_*ero 0

这都是关于多态性的。ArrayList 是接口 IList的实现。

 IList iList = new ArrayList();  
Run Code Online (Sandbox Code Playgroud)

变量 iList 的静态类型是IList,但它引用了 ArrayList 对象!

没有从 IList 到 ArrayList 的真正转换,因为您无法从接口或抽象类实例化/创建对象。