Favorite way to create an new IEnumerable<T> sequence from a single value?

Marcel Lamothe picture Marcel Lamothe · Jun 19, 2009 · Viewed 80.4k times · Source

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

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

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

Answer

JaredPar picture JaredPar · Jun 19, 2009

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>();

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

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