【发布时间】:2009-07-12 22:54:05
【问题描述】:
我的情况是,我只想将字符串数组(类型 String[])中的值附加到具有 IList
【问题讨论】:
我的情况是,我只想将字符串数组(类型 String[])中的值附加到具有 IList
【问题讨论】:
因为接口通常是使其可用所需的最少功能,以减轻实现者的负担。使用 C# 3.0,您可以将其添加为扩展方法:
public static void AddRange<T>(this IList<T> list, IEnumerable<T> items) {
if(list == null) throw new ArgumentNullException("list");
if(items == null) throw new ArgumentNullException("items");
foreach(T item in items) list.Add(item);
}
等等; IList<T> 现在有AddRange:
IList<string> list = ...
string[] arr = {"abc","def","ghi","jkl","mno"};
list.AddRange(arr);
【讨论】: