喜欢的方式来创建一个新的IEnumerable< T>从一个单一的值序列? [英] Favorite way to create an new IEnumerable<T> sequence from a single value?

查看:76
本文介绍了喜欢的方式来创建一个新的IEnumerable< T>从一个单一的值序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常从一个单一的值使用数组语法创建序列,像这样的:

I usually create a sequence from a single value using array syntax, like this:

IEnumerable<string> sequence = new string[] { "abc" };

或使用新的列表。我想听到的话,任何人有更多的前pressive的方式做同样的事情。

Or using a new List. I'd like to hear if anyone has a more expressive way to do the same thing.

推荐答案

您的例子不是一个空序列,它与一个元素的序列。要创建一个字符串的空序列,你可以做

Your example is not an empty sequence, it's a sequence with one element. To create an empty sequence of strings you can do

var sequence = Enumerable.Empty<string>();

修改 OP澄清他们在寻找建立一个单一的价值。在这种情况下

EDIT OP clarified they were looking to create a single value. In that case

var sequence = Enumerable.Repeat("abc",1);

这篇关于喜欢的方式来创建一个新的IEnumerable&LT; T&GT;从一个单一的值序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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