我的问题如上所述。例如
IEnumerable<T> items = new T[]{new T("msg")};
items.ToList().Add(new T("msg2"));
但毕竟里面只有一项。我们能不能有一个items.Add(item)方法,比如List<T>?
我的问题如上所述。例如
IEnumerable<T> items = new T[]{new T("msg")};
items.ToList().Add(new T("msg2"));
但毕竟里面只有一项。我们能不能有一个items.Add(item)方法,比如List<T>?
当前回答
也许我说得太迟了,但我希望这对将来的任何人都有帮助。
可以使用insert函数在特定索引处添加项。 列表。插入(0项);
其他回答
关于为什么不能(也不应该!)向IEnumerable中添加项,其他人已经给出了很好的解释。我只会补充说,如果您希望继续为表示集合的接口编码,并且想要一个add方法,那么您应该为ICollection或IList编码。作为额外的财源,这些接口实现了IEnumerable。
当然,你可以(我把你的T-business放在一边):
public IEnumerable<string> tryAdd(IEnumerable<string> items)
{
List<string> list = items.ToList();
string obj = "";
list.Add(obj);
return list.Select(i => i);
}
最简单的方法很简单
IEnumerable<T> items = new T[]{new T("msg")};
List<string> itemsList = new List<string>();
itemsList.AddRange(items.Select(y => y.ToString()));
itemsList.Add("msg2");
然后你可以返回list作为IEnumerable,因为它实现了IEnumerable接口
不,IEnumerable不支持向它添加项目。另一种解决方案是
var myList = new List(items);
myList.Add(otherItem);
很抱歉,我又提了一个老问题,但由于它出现在谷歌搜索结果的第一个列表中,我想有些人会一直登陆这里。
在许多答案中,其中一些非常有价值,并且解释得很好,我想补充一个不同的观点,因为对我来说,问题还没有很好地确定。
你正在声明一个存储数据的变量,你需要它能够通过添加项目来改变吗?所以你不应该把它声明为IEnumerable。
由@NightOwl888提议
对于本例,只需声明IList而不是IEnumerable: IList items = new T[]{new T("msg")};物品。添加(新T(“msg2”));
试图绕过声明的接口限制只表明您做出了错误的选择。 除此之外,所有提议用来实现其他实现中已经存在的东西的方法都应该考虑。 允许您添加项的类和接口已经存在。为什么总是复制别人已经做过的东西?
这种考虑是在接口中抽象变量功能的目标。
TL;DR:在我看来,这些是做你需要的事情的最干净的方法:
// 1st choice : Changing declaration
IList<T> variable = new T[] { };
variable.Add(new T());
// 2nd choice : Changing instantiation, letting the framework taking care of declaration
var variable = new List<T> { };
variable.Add(new T());
当您需要使用变量作为IEnumerable时,您将能够。当你需要将它作为数组使用时,你可以调用'ToArray()',它总是应该这么简单。不需要扩展方法,仅在真正需要时才强制转换,能够在变量上使用LinQ,等等…
停止做奇怪和/或复杂的事情,因为你只是在声明/实例化时犯了一个错误。