你能帮我理解这两者之间的实际差异吗?
IList<int> myList = new List<int>();
List<int> myList = new List<int>();
Run Code Online (Sandbox Code Playgroud)
Jus*_*ner 44
IList<int> myList = new List<int>();
Run Code Online (Sandbox Code Playgroud)
将仅公开接口公开的方法 IList
List<int> myList = new List<int>();
Run Code Online (Sandbox Code Playgroud)
将公开该List对象的所有成员
我不会在此列出所有差异,但您可以查看MSDN上的成员列表以获取每个成员列表的完整列表:
api*_*guy 13
当你做这样的事情时,真正的区别更容易看到:
IList<int> myList;
if (x == true)
myList = getBindngList();
else
myList = getList();
public BindingList<int> getBindingList()
{
// do important things...
return new BindingList<int>();
}
public List<int> getList()
{
// do important things...
return new List<int>();
}
Run Code Online (Sandbox Code Playgroud)
因为List和BindingList都实现了IList接口,所以可以通过类型为IList的变量访问它们.如果你的变量是一个List,并且返回你想要的数据的方法返回了一个BindingList,那么你必须走硬路并将返回的BindingList的所有成员添加到一个新的List中,或者只写另一个方法.