Why we don’t recommend using List<T> in public APIs
不推荐List<T>做API
原因有如下两点:
1.首先List<T> 设计之初就没有设计成可扩展的,我们不能重新其任何方法。这就意味着,我们操作List<T>的时候却不
能有任何的通知机制,而Collection<T>却提供了SetItem虚方法以供重写,以便于我们在修改成员信息或者添加成员的
时候可以自定义实现通知机制。
2.其次List<T>有许多的成员在很多的使用场景下是相关联的,这就是我们所说的List<T>对外公开了过多的成员方法,
想象下,ListView中的Items属性返回(包含了众多属性的)List<T>。来看看实际上ListView.Items的返回类型;它的方
式更简单,类似于Collection<T>或ReadOnlyCollection<T>。
由于List<T>与Collection<T>的内容比较多,大家感兴趣可以自己查看下,这里只把Collection<T>的部分片段
列出来了。
public class Collection<T> : IList<T>, IList, IReadOnlyList<T>
{
protected virtual void ClearItems()
protected virtual void InsertItem(int index, T item)
protected virtual void RemoveItem(int index)
protected virtual void SetItem(int index, T item)
}
而且其公开方法大概也只有10来个,而List<T>的公开发方法粗略的估计一下至少也得有30个以上。所以明显的可以看出
来,对于系统内部使用为了方便,List<T>提供了更便利的操作,而且在性能上,存储上做了大量的优化。但是如果作为
API显然有些过于庞大和不可控了。
(原文:http://blogs.msdn.com/b/kcwalina/archive/2005/09/26/474010.aspx)
Why we don’t recommend using List<T> in public APIs的更多相关文章
- 不要对外公开泛型List成员
最近在阅读Framework Design Guidelines,本着现学现用的原则,于是就用FxCop工具对代码进行规范性检查时,发现了很多问题,其中包括命名以及一些设计上的规范. 其中,Do no ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- Designing a Secure REST (Web) API without OAuth
原文:http://www.thebuzzmedia.com/designing-a-secure-rest-api-without-oauth-authentication/ Situation Y ...
- How to Write Doc Comments for the Javadoc Tool
http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html This document describe ...
- Oracle Applications Multiple Organizations Access Control for Custom Code
档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code ...
- 设计模式浅谈----策略模式(c#及java实现)
一.何为策略模式 策略模式是行为型模式的一种,主要用于需要使用不同的算法来处理不同的数据对象时使用,是一种可以在运行时选择算法的设计模式.也称为政策模式. 主要解决:在有多种算法相似的情况下,使用 i ...
- Span<T>
Introduction Span<T> is a new type we are adding to the platform to represent contiguous regio ...
- Google C++ 代码规范
Google C++ Style Guide Table of Contents Header Files Self-contained Headers The #define Guard For ...
- API Design Principles -- QT Project
[the original link] One of Qt’s most reputed merits is its consistent, easy-to-learn, powerfulAPI. T ...
随机推荐
- Palindrome Linked List leetcode
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- 每天一个Linux命令 6
rpm包管理--yum在线管理 ip地址配置和网络yum源ip地址配置 #setup 使用setup工具 #vi /etc/sysconfig/network-scripts/ifcfg-eth0把O ...
- ThinkPhp框架:验证码功能
Think\Verify类可以支持验证码的生成和验证功能. 为了显示这个验证码功能,第一要有控制器,再就是有方法,然后是显示的页面. 一.最简单的方式生成验证码 (1)我们还是继续在那个控制器编写方法 ...
- Go语言并发机制初探
Go 语言相比Java等一个很大的优势就是可以方便地编写并发程序.Go 语言内置了 goroutine 机制,使用goroutine可以快速地开发并发程序, 更好的利用多核处理器资源.这篇文章学习 g ...
- css中设置div垂直水平居中的方法
设置要水平垂直居中的div的position为absolute,left:50%;margin-left为负的这个元素宽度的一半,同理,top:50%;margin-top为负的这个元素的高度的一半. ...
- 提问!同一ajax请求获取的图片路劲,在谷歌浏览器能正确展示图片,在火狐浏览器则显示路径undefined
今天的工作学习之路遇见一个奇葩的问题,作为初级攻城狮的小生实在不知如何解决,都已经壁咚度娘一整天了,都未能解决问题,实属无奈,一开始认为是浏览器兼容的问题,但左看右看,也不是,也尝试过是不是页面加载与 ...
- 去除IOS浏览器下面的工具栏
在head标签里添加下面的元素 即可 <meta id="viewport" name="viewport" content="width=de ...
- (转)crontab安装(command not found)
1. 确认crontab是否安装:执行 crontab -l 命令如果报 command not found,就表明没有安装 2. 安装 crontab执行 yum install -y vixie- ...
- iOS 任务的依赖操作
-(void)dependency{ /** 假设有A.B~C三个操作,要求: 1. 3个操作都异步执行 2. 操作C依赖于操作B 3. 操作B依赖于操作A */ //创建一个队列 NSOperati ...
- Nginx编译参数
configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector //设置额外的参数将被添加到CFLAGS变量.(FreeBSD或者 ...