什么是最好的京顺路添加单个元素添加到IEnumerable集合? [英] What is the best wayto add single element to an IEnumerable collection?

查看:119
本文介绍了什么是最好的京顺路添加单个元素添加到IEnumerable集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶地看到,似乎没有成为一个方法,一个单一的元素添加到IEnumerable集合。

I'm surprised to see that there does not appear to be a method to add a single element to an IEnumerable collection.

我如何添加一个单一的元素添加到IEnumerable集合?

How can I add a single element to an IEnumerable collection?

推荐答案

您真的不能添加元素为IEnumerable因为它应该是只读的。最好的办法是要么是这样的:

You can't really add elements to an IEnumerable as it's supposed to be read only. Your best bet is either something like:

return new List<Foo>(enumerable){ new Foo() };

return enumerable.Concat(new [] { new Foo() });

这篇关于什么是最好的京顺路添加单个元素添加到IEnumerable集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆