IEnumerable有两个接口形式,非泛型的IEnumerable在System.Collections中,泛型的IEnumerable<T>在System.Collections.Generic中。
IEnumerable<T>由于是强类型对象,因此使用对象成员比较方便,
IEnumerable是弱类型对象,作为虚参可以适应更多的对象,然后可以使用Cast<T>()和OfType<T>()的扩展方法转换为强类型集合。
曾经写过一个函数,
void SetCollection(IEnumerable<object> items)
当items的实参是Dictionary时,不能隐式转换为IEnumerable<object>,因为Dictionary是KeyValuePair的集合,KeyValuePair是struct,不是class,所以转换失败。
使用非泛型的参数就可以了
void SetCollection(IEnumerable items)