C# 将一项添加到 IEnumerable<T> 的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15251159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the Best Way to Add One Item to an IEnumerable<T>?
提问by user2023861
Here's how I would add one item to an IEnumerable object:
这是我将一项添加到 IEnumerable 对象的方法:
//Some IEnumerable<T> object
IEnumerable<string> arr = new string[] { "ABC", "DEF", "GHI" };
//Add one item
arr = arr.Concat(new string[] { "JKL" });
This is awkward. I don't see a method called something like ConcatSingle()
however.
这很尴尬。我没有看到类似的方法ConcatSingle()
。
Is there a cleaner way to add a single item to an IEnumerable object?
是否有更简洁的方法将单个项目添加到 IEnumerable 对象?
采纳答案by LukeH
Nope, that's about as concise as you'll get using built-in language/framework features.
不,这与您使用内置语言/框架功能所获得的一样简洁。
You could always create an extension method if you prefer:
如果您愿意,您始终可以创建扩展方法:
arr = arr.Append("JKL");
// or
arr = arr.Append("123", "456");
// or
arr = arr.Append("MNO", "PQR", "STU", "VWY", "etc", "...");
// ...
public static class EnumerableExtensions
{
public static IEnumerable<T> Append<T>(
this IEnumerable<T> source, params T[] tail)
{
return source.Concat(tail);
}
}
回答by cuongle
IEnumerable
is immutablecollection, it means you cannot add, or remove item. Instead, you have tocreate a new collection for this, simply to convert to list to add:
IEnumerable
是不可变集合,这意味着您不能添加或删除项目。相反,您必须为此创建一个新集合,只需转换为列表即可添加:
var newCollection = arr.ToList();
newCollection.Add("JKL"); //is your new collection with the item added
回答by Daniel Hilgarth
Write an extension method ConcatSingle
:)
写一个扩展方法ConcatSingle
:)
public static IEnumerable<T> ConcatSingle<T>(this IEnumerable<T> source, T item)
{
return source.Concat(new [] { item } );
}
But you need to be more careful with your terminology.
You can't add an item to an IEnumerable<T>
. Concat
creates a new instance.
但是你需要更加小心你的术语。
您不能将项目添加到IEnumerable<T>
. Concat
创建一个新实例。
Example:
例子:
var items = Enumerable.Range<int>(1, 10)
Console.WriteLine(items.Count()); // 10
var original= items;
items = items.ConcatSingle(11);
Console.WriteLine(original.Count()); // 10
Console.WriteLine(items.Count()); // 11
As you can see, the original enumeration - which we saved in original
didn't change.
如您所见,我们保存的原始枚举original
没有改变。
回答by Soner G?nül
Since IEnumerable
is read-only, you need to convert to list.
由于IEnumerable
是只读的,您需要转换为列表。
var new_one = arr.ToList().Add("JKL");
Or you can get a extension methodlike;
或者你可以得到一个扩展方法,比如;
public static IEnumerable<T> Append<T>(this IEnumerable<T> source, params T[] item)
{
return source.Concat(item);
}
回答by Caelan
You're assigning an array to an IEnumerable. Why don't you use the Array type instead of IEnumerable?
您正在将数组分配给 IEnumerable。为什么不使用 Array 类型而不是 IEnumerable?
Otherwise you can use IList (or List) if you want to change the collection.
否则,如果要更改集合,可以使用 IList(或 List)。
I use IEnumerable only for methods params when I need to read and IList (or List) when I need to change items in it.
当我需要阅读时,我只将 IEnumerable 用于方法参数,当我需要更改其中的项目时,我将 IList(或 List)用于方法参数。