为IEnumerable类型添加Add方法

IEnumerable类型原生是没有Add方法的,你可以用Contact方法去为它添加元素,

items = items.Concat(new[] { "foo" });

也可以用个扩展方法:

public static IEnumerable<T> Add<T>(this IEnumerable<T> e, T value) {
  foreach ( var cur in e) {
    yield return cur;
  }
  yield return value;
}
原文地址:https://www.cnblogs.com/walkerwang/p/2651277.html