我知道IList是接口,List是具体类型,但我仍然不知道何时使用每一个。我现在做的是,如果我不需要Sort或FindAll方法,我使用接口。我说的对吗?是否有更好的方法来决定何时使用接口或具体类型?
当前回答
If you're working within a single method (or even in a single class or assembly in some cases) and no one outside is going to see what you're doing, use the fullness of a List. But if you're interacting with outside code, like when you're returning a list from a method, then you only want to declare the interface without necessarily tying yourself to a specific implementation, especially if you have no control over who compiles against your code afterward. If you started with a concrete type and you decided to change to another one, even if it uses the same interface, you're going to break someone else's code unless you started off with an interface or abstract base type.
其他回答
你应该只在你需要它的时候使用这个接口,例如,如果你的列表被强制转换为一个IList实现而不是list。这是真的,例如,当你使用NHibernate,它在检索数据时将列表转换为NHibernate包对象。
如果List是您将用于某个集合的唯一实现,则可以将其声明为具体的List实现。
我同意李的建议,接受一些参数,但不要回头。
如果你指定你的方法来返回一个接口,这意味着你以后可以自由地更改确切的实现,而消费方法永远不会知道。我认为我永远不需要从List<T>更改,但后来不得不更改为使用自定义列表库,以获得它提供的额外功能。因为我只返回了一个IList<T>,所以使用这个库的人都不需要改变他们的代码。
当然,这只需要应用于外部可见的方法(即公共方法)。我个人甚至在内部代码中使用接口,但如果你做了破坏性的更改,你可以自己更改所有的代码,这并不是严格必要的。
If you're working within a single method (or even in a single class or assembly in some cases) and no one outside is going to see what you're doing, use the fullness of a List. But if you're interacting with outside code, like when you're returning a list from a method, then you only want to declare the interface without necessarily tying yourself to a specific implementation, especially if you have no control over who compiles against your code afterward. If you started with a concrete type and you decided to change to another one, even if it uses the same interface, you're going to break someone else's code unless you started off with an interface or abstract base type.
有一件重要的事情,人们似乎总是忽视:
你可以将一个普通数组传递给接受IList<T>参数的对象,然后你可以调用IList. add(),并将收到一个运行时异常:
未处理异常:系统。NotSupportedException:集合大小固定。
例如,考虑以下代码:
private void test(IList<int> list)
{
list.Add(1);
}
如果你像下面这样调用它,你会得到一个运行时异常:
int[] array = new int[0];
test(array);
这是因为使用带有IList<T>的普通数组违反了利斯科夫替换原则。
因此,如果你调用IList<T>. add(),你可能需要考虑使用List<T>而不是IList<T>。
在我通常遇到的情况下,我很少直接使用IList。
通常我只是把它用作方法的参数
void ProcessArrayData(IList almostAnyTypeOfArray)
{
// Do some stuff with the IList array
}
这将允许我对. net框架中的几乎任何数组进行泛型处理,除非它使用IEnumerable而不是IList,这种情况有时会发生。
这实际上取决于你需要什么样的功能。我建议在大多数情况下使用List类。当你需要创建一个自定义数组,其中可能包含一些非常特定的规则,你希望将这些规则封装在一个集合中,这样你就不会重复自己的操作,但仍然希望. net将其识别为一个列表时,IList是最好的选择。
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 为什么Visual Studio 2015/2017/2019测试运行器没有发现我的xUnit v2测试
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 没有ListBox。SelectionMode="None",是否有其他方法禁用列表框中的选择?
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何在iis7应用程序池中设置。net Framework 4.5版本