105

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.

Marcel Lamothe
  • 11,734
  • 8
  • 33
  • 42

4 Answers4

151

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);
JaredPar
  • 703,665
  • 143
  • 1,211
  • 1,438
72

I like what you suggest, but with the array type omitted:

var sequence = new[] { "abc" };
Bryan Watts
  • 43,667
  • 15
  • 80
  • 87
23

Or even shorter,

string[] single = { "abc" };

I would make an extension method:

public static T[] Yield<T>(this T item)
{
    T[] single = { item };
    return single;
}

Or even better and shorter, just

public static IEnumerable<T> Yield<T>(this T item)
{
    yield return item;
}

Perhaps this is exactly what Enumerable.Repeat is doing under the hood.

nawfal
  • 66,413
  • 54
  • 311
  • 354
  • 1
    The last one is brilliant. Except for the name... it will conflict with types that already implement IEnumerable such as the string in your example. Try .AsSingleItemEnumerable(), or simply .Yield() --> "abc".Yield() – DanO Apr 04 '13 at 17:23
  • 8
    I think ToEnumerable is more appropriate. – Zodman Sep 04 '13 at 12:37
  • 2
    +1 `Yield` is good. I made `IEnumerable Yield(this T source, params T[] others)` too. – Jodrell Apr 07 '14 at 16:13
  • I tried to do away with Yield altogether in favour of a lambda but somehow it never compiled... cf. http://stackoverflow.com/questions/1217729/in-c-why-cant-an-anonymous-method-contain-a-yield-statement ;-). – Peter - Reinstate Monica Nov 17 '15 at 16:26
  • @PeterSchneider how and why did you do that? Without seeing code I cannot comment. I dont think I follow you. – nawfal Nov 17 '15 at 17:42
  • Well, I simply played around, today, for no good reason. I wanted to pass e.g. `(item) => yield return item;` to a function which expects an IEnumerable, or define a Func> = () => yield return 1;` or such. But it's impossible to do that for the reasons mentioned in the accepted answer of the question I pointed to. – Peter - Reinstate Monica Nov 17 '15 at 18:00
  • @PeterSchneider that's right. I got you now. C# doesn't let you do that. Instead, if you have the above Yield method as in the answer you could pass `1.Yield()` to the function. Or to define anonymous function, you do: `Func> = () => 1.Yield();` – nawfal Nov 17 '15 at 18:44
7

or just create a method

public static IEnumerable<T> CreateEnumerable<T>(params T[] items)
{
    if(items == null)
        yield break;

    foreach (T mitem in items)
        yield return mitem;
}

or

public static IEnumerable<T> CreateEnumerable<T>(params T[] items)
{
   return items ?? Enumerable.Empty<T>();
}

usage :

IEnumerable<string> items = CreateEnumerable("single");
Andrew
  • 194
  • 4
  • 6