任何人都可以向我解释为什么我想在C#中使用IList而不是List?
相关问题:为什么暴露它被认为是不好的List<T>
请参阅System.Array类的定义
public abstract class Array : IList, ...
Run Code Online (Sandbox Code Playgroud)
从理论上讲,我应该能够写下这一点,并感到高兴
int[] list = new int[] {};
IList iList = (IList)list;
Run Code Online (Sandbox Code Playgroud)
我也应该可以从iList中调用任何方法
ilist.Add(1); //exception here
Run Code Online (Sandbox Code Playgroud)
我的问题不是为什么我得到一个例外,而是为什么Array实现了IList?
可能重复:
C# - List <T>或IList <T>
当我从我的方法返回一个列表时,我可以用两种方式完成.作为一个清单
Private List<datatype> MethodName()
{
Return List
}
Run Code Online (Sandbox Code Playgroud)
作为一个IList
Private IList<datatype> MethodName()
{
Return IList
}
Run Code Online (Sandbox Code Playgroud)
我听说我们应该把它作为IList归还.有人能解释为什么吗?