C#中List<T> 和 IList<T> 的比较
来源:互联网 发布:微博搞笑排行榜 知乎 编辑:程序博客网 时间:2024/04/28 02:10
从stackoverflow上找的几个观点:
【1】
IEnumerable<T>
allows you to iterate through a collection.
ICollection<T>
builds on this and also allows for adding and removing items.
IList<T>
also allows for accessing and modifying them at a specific index. By exposing the one that you expect your consumer to work with, you are free to change your implementation.
List<T>
happens to implement all three of those interfaces.
If you expose your property as a List<T>
or even an IList<T>
when all you want your consumer to have is the ability to iterate through the collection. Then they could come to depend on the fact that they can modify the list.
There are two rules I follow:
- Accept the most basic type that will work
- Return the richest type your user will need
So when writing a function or method that takes a collection, write it not to take a List, but an IList<T>, an ICollection<T>, or IEnumerable<T>. The generic interfaces will still work even for heterogenous lists because System.Object can be a T too. Doing this will save you headache if you decide to use a Stack or some other data structure further down the road. If all you need to do in the function is foreach through it, IEnumerable<T> is really all you should be asking for.
On the other hand, when returning an object out of a function, you want to give the user the richest possible set of operations without them having to cast around. So in that case, if it's a List<T> internally, return a copy as a List<T>.
【3】
There are three questions here: what type should I use for a formal parameter? What should I use for a local variable? and what should I use for a return type?
Formal parameters:
The principle here is do not ask for more than you need.
IEnumerable<T>
communicates "I need to get the elements of this sequence from beginning to end".
IList<T>
communicates "I need to get and set the elements of this sequence in arbitrary order".
List<T>
communicates "I need to get and set the elements of this sequence in arbitrary order and I only accept lists; I do not accept arrays."
By asking for more than you need, you
(1) make the caller do unnecessary work to satisfy your unnecessary demands, and
(2) communicate falsehoods to the reader. Ask only for what you're going to use. That way if the caller has a sequence, they don't need to call ToList on it to satisfy your demand.
Local variables:
Use whatever you want. It's your method. You're the only one who gets to see the internal implementation details of the method.
Return type:
Same principle as before, reversed. Offer the bare minimum that your caller requires. If the caller only requires the ability to enumerate the sequence, only give them an IEnumerable<T>
.
【4】
The most practical reason I've ever seen was given by Jeffrey Richter in CLR via C#.
The pattern is to take the basest class or interface possible for your arguments and return the most specific class or interface possible for your return types. This gives your callers the most flexibility in passing in types to your methods and the most opportunities to cast/reuse the return values.
- C#中List<T> 和 IList<T> 的比较
- C#中IList<T>与List<T>的区别
- C#中IList<T>与List<T>的区别感想
- List<T>和Ilist<T>的区别
- C#编程之IList<T>、List<T>和ArrayList
- IList<T>和List<T>区别
- List<T>与IList<T>的区别
- C#中List 和 IList的区别
- C# 中 Ilist<T> 转换成 DataTable
- C#中数组Array、ArrayList、泛型List<T>的比较
- C#中List<T>
- Ilist<T>转换为List<T>
- C#中List<T>的用法
- C# List<T>中Find的用法
- C#中IList<>和List<>的区别小结
- c#学习笔记对IEnumerable<T>,IDictionary<Tkey,TValue>,ICollection<T>,IList<T>的总结
- C# List<T> Contains<T>() 的用法
- List<T>和IList<T>、string.Equals、trim()、web相对路径
- 分布式数据库拆表拆库的常用策略
- win7下安装Python +matplotlib
- 将字符串转化为数组
- 第7周上机实践项目1——电阻串联
- GitHub for Windows 2.0使用教程
- C#中List<T> 和 IList<T> 的比较
- shell中的for循环
- 9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路
- 小米平板 粉色版 16GB,不可以听书
- 全自动软化水设备:全自动软化水装置性能剖析
- JAVA的Collections类中shuffle的用法 (将列表值打乱顺序)
- ...刘烨晒与混血儿子合影
- 利用ParameterizedType和范型做切面编程
- Android自定义控件——3D画廊和图像矩阵