107

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
  • 12,452
  • 9
  • 34
  • 44

4 Answers4

154

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
  • 733,204
  • 149
  • 1,241
  • 1,454
77

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

var sequence = new[] { "abc" };
Bryan Watts
  • 44,911
  • 16
  • 83
  • 88
  • 4
    I never realized that you could do that. I think this is clearer than using Repeat. – Marcel Lamothe Jun 20 '09 at 15:45
  • 2
    But this isn't an IEnumerable, it's an array! – Zodman Sep 04 '13 at 11:40
  • 6
    @Bryan Watts I know that, but it's a concrete implementation of an IEnumerable. The question is asking for an IEnumerable<> (even if the example isn't). There's a difference. – Zodman Sep 04 '13 at 22:09
  • Maybe the question should be changed. – Zodman Sep 04 '13 at 22:10
  • the reason i researched this was trying to mock a method that returns `Task>`. So this doesn't help. – Jonesopolis Oct 31 '16 at 18:44
  • 1
    @Jonesopolis: That is a different, unrelated situation. You can use `Task.FromResult` to achieve that. – Bryan Watts Nov 02 '16 at 17:58
  • @Jonesopolis, If you are in a situation where you want the type to be strictly `IEnumerable` (and not just some specific type that implements it), then it may help to do `new[] { "abc" }.AsEnumerable()` instead. An example could be `Task.FromResult(new[] { "abc" }.AsEnumerable())` where the type argument to the `FromResult<>` method is _inferred_. This is shorter than `Task.FromResult>(new[] { "abc" })` (and when the type name is more complex than just `string`, it will be even more economical). – Jeppe Stig Nielsen Jul 04 '23 at 11:57
25

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
  • 70,104
  • 56
  • 326
  • 368
  • 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
  • 1
    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
8

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
  • 344
  • 5
  • 7