您不能,因为
IEnumerable<T>不一定代表可以添加项目的集合。实际上,它不一定完全代表一个集合!例如:
IEnumerable<string> ReadLines(){ string s; do { s = Console.ReadLine(); yield return s; } while (!string.IsNullOrEmpty(s));}IEnumerable<string> lines = ReadLines();lines.Add("foo") // so what is this supposed to do??但是,您可以做的是创建一个 新
IEnumerable对象(未指定类型),该对象在枚举时将提供旧对象的所有项目,再加上您自己的一些项目。您
Enumerable.Concat为此使用:
items = items.Concat(new[] { "foo" });这 不会更改数组对象 (无论如何您都不能将项目插入到数组中)。但是它将创建一个新对象,该对象将列出阵列中的所有项目,然后列出“
Foo”。此外,该新对象将 跟踪数组中的更改 (即,只要枚举它,您将看到项目的当前值)。



