IEnumerable扩展支持Add,Remove等操作
1 public static class CollectionExtend 2 { 3 /// <summary> 4 /// 向集合中添加元素 5 /// </summary> 6 /// <typeparam name="T"></typeparam> 7 /// <param name="collection"></param> 8 /// <param name="value"></param> 9 public static void Add<T>(this IEnumerable<T> collection, T value) 10 { 11 var list = collection as List<T>; 12 if (list != null) list.Add(value); 13 } 14 15 /// <summary> 16 /// 从集合中删除元素 17 /// </summary> 18 /// <typeparam name="T"></typeparam> 19 /// <param name="collection"></param> 20 /// <param name="value"></param> 21 public static void Remove<T>(this IEnumerable<T> collection, T value) 22 { 23 var list = collection as List<T>; 24 if (list != null) list.Remove(value); 25 } 26 /// <summary> 27 /// 检索集合中是否包含某个元素 28 /// </summary> 29 /// <typeparam name="T"></typeparam> 30 /// <param name="collection"></param> 31 /// <param name="value"></param> 32 /// <returns></returns> 33 public static bool Contains<T>(this IEnumerable<T> collection, T value) 34 { 35 var list = collection as List<T>; 36 return list != null && list.Contains(value); 37 } 38 }