Search code examples
c#linqc#-3.0

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


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.


Solution

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