在LINQ前,先回顾下集合:
集合基于ICollection、IList、IDictionary、IEnuerable接口及其泛型版本,例如IEnuerable<T>,IList和IDictionary均从ICollection接口派生,而ICollection接口又从IEnumerable接口派生。而泛型版本IEnumerable<T>也派生自IEnumerable,相当于集合直接或间接的派生自IEnumerable接口。
借用一张别人的集合接口图:
作为父接口,IEnumerable接口没有上级父接口,IEnumerable只有一个方法GetEnumerator(),凡继承自此接口且实现此方法的集合均可以使用foreach语句进行遍历。
集合数组:
Array:长度固定,可多维度,不可伸缩;可读可写,不能声明只读Array;支持下标访问,一对一获取设置元素值;必须声明元素类型。
ArrayList:长度可变,速度较慢,本质还是Array,可针对包括null在内的任意元素,非强类型。
ArrayList myArrayList = new ArrayList();
Object obj = null; myArrayList.Add(obj); // 元素类型为Object,可为null
myArrayList.Add(null); // 元素可为空null
myArrayList.A