列表<T>或 IList T [英] List&lt;T&gt; or IList&lt;T&gt;

查看:31
本文介绍了列表<T>或 IList T的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能向我解释为什么我想在 C# 中使用 IList 而不是 List?

Can anyone explain to me why I would want to use IList over List in C#?

相关问题:为什么公开List

推荐答案

如果您通过其他人将使用的库公开您的类,您通常希望通过接口而不是具体实现公开它.如果您决定稍后更改类的实现以使用不同的具体类,这将有所帮助.在这种情况下,您图书馆的用户不需要更新他们的代码,因为界面不会改变.

If you are exposing your class through a library that others will use, you generally want to expose it via interfaces rather than concrete implementations. This will help if you decide to change the implementation of your class later to use a different concrete class. In that case the users of your library won't need to update their code since the interface doesn't change.

如果只是在内部使用,可能不会太在意,使用List可能没问题.

If you are just using it internally, you may not care so much, and using List<T> may be ok.

这篇关于列表<T>或 IList T的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆