C# IList<int> 与 List<int>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1367539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:43:38  来源:igfitidea点击:

IList<int> vs List<int>

c#

提问by kaivalya

Can you help me understand the practicaldifferences between these two;

你能帮我理解这两者之间的实际区别吗?

IList<int> myList = new List<int>();

List<int> myList = new List<int>();

采纳答案by Justin Niessner

IList<int> myList = new List<int>();

will only expose the methods exposed by the interface IList

只会暴露接口暴露的方法 IList

List<int> myList = new List<int>();

will expose all members of the Listobject

将暴露List对象的所有成员

I'm not going to list all the differences here, but you can check out the member lists on MSDN for a complete list for each:

我不会在这里列出所有差异,但您可以查看 MSDN 上的成员列表以获取每个差异的完整列表:

IList(T) Members
List(T) Members

IList(T) 成员
List(T) 成员

回答by bernhardrusch

IList won't get you all the functions you get with List.

IList 不会为您提供使用 List 获得的所有功能。

IList is an interface and is implemented by many list - like classes - one of those classes ist List.

IList 是一个接口,由许多列表实现——比如类——其中一个类就是 List。

回答by Tomas Aschan

Your first example will onlybe usable as an IList<int>without "casting" using the assyntax, whereas your second example will also be usable as an IEnumerable<int>and ICollection<int>right away.

您的第一个示例只能用作IList<int>不使用as语法的“强制转换” ,而您的第二个示例也可以立即用作 anIEnumerable<int>ICollection<int>

回答by Lucero

Basically, IList<>can by implemented by any class (one of which is List<>). Therefore, using IList<>for parameters and properties makes the coupling more loose, which is usually a good thing, since you are not restricted to one specific implementation.

基本上,IList<>可以由任何类(其中之一是List<>)实现。因此,使用IList<>for 参数和属性会使耦合更加松散,这通常是一件好事,因为您不受限于一个特定的实现。

回答by Eric Petroelje

If you use the second form, you'll be able to call methods on myList that are part of the List class, but not part of the IList interface without a cast.

如果您使用第二种形式,您将能够调用 myList 上属于 List 类的方法,但在没有强制转换的情况下不能调用 IList 接口的一部分。

If you use the first form, you'd be able to assign other instances of IList to myList in case you wanted to use a different IList implementation later.

如果您使用第一种形式,您将能够将 IList 的其他实例分配给 myList,以防您以后想使用不同的 IList 实现。

When deciding which one to use, the best practice is generally to use the most generic form possible that has all the functionality you need. If IList has all the methods you need, then use that. If IEnumerable has everything you need, then you could be even more generic and use that instead.

在决定使用哪一种时,最佳实践通常是使用具有您需要的所有功能的最通用的形式。如果 IList 具有您需要的所有方法,请使用它。如果 IEnumerable 拥有您需要的一切,那么您可以更通用并改用它。

回答by apiguy

The real difference is easier to see when you do something like this:

当你做这样的事情时,真正的区别更容易看出:

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>();
}

Because both List and BindingList implement the IList interface, you can access them through a variable typed as IList. If your variable was a List, and the method that returned the data you wanted returned a BindingList, well you'd have to take the hard road and add all of the members of the returned BindingList to a new List or just write another method.

因为 List 和 BindingList 都实现了 IList 接口,所以您可以通过一个类型为 IList 的变量来访问它们。如果您的变量是一个 List,并且返回您想要的数据的方法返回了一个 BindingList,那么您必须走艰难的道路,将返回的 BindingList 的所有成员添加到一个新的 List 中,或者只编写另一个方法。

回答by mahendra

I will give you a very simple example

我会给你一个非常简单的例子

Lets say we have a class library and we have class1as class

假设我们有一个类库,我们有一个class1

It has 2 methods

它有2种方法

public IList getList()
{
    IList mylist=new List();
    mylist.add("hello");
    mylist.add("hi");
    return mylist;
}

public List getList()
{
List mylist=new List();
mylist.add("hello");
mylist.add("hi");
return mylist;
}

Now when the class library is being used. and it happens than you need to change the Collection from List to Array or indexed type.

现在当正在使用类库时。发生这种情况时,您需要将集合从列表更改为数组或索引类型。

We can do this like

我们可以这样做

public IList getList()
{
    IList mylist=new int[10];
    lst[0] = 10;
    return mylist;
}

But We cannot do this

但我们不能这样做

public List getList()
{
    List mylist=new int[10];
    lst[0] = 10;
    return mylist;
}