【发布时间】:2011-08-29 14:09:42
【问题描述】:
Q1为什么 .NET 的新类仅部分实现接口?
Q2我应该在我的代码中做同样的事情吗?
我问了这个问题here,所以我想,好吧,那是很久以前的事了,你可以有不同的用法等等,现在这种实现只是出于一致性的原因才被支持。但新课程也可以做到这一点。
旧
int[] list = new int[] {};
IList iList = (IList)list;
ilist.Add(1); //exception here
新
ICollection c = new ConcurrentQueue<int>();
var root = c.SyncRoot; //exception here
更新
我并不担心为什么我会遇到异常,这很清楚。但我不明白为什么类实现定义明确的合同,而不是所有成员(这会导致令人不快的运行时异常)?
【问题讨论】:
标签: c# .net collections interface