return single instance object as IEnumerable

12,414

Solution 1

Options:

  • Create an instance of a collection class, like an array or a list. This would be mutable by default, which would be slightly unhelpful if this is a sequence you want to be able to hand out in your API. You could create a ReadOnlyCollection<T> wrapper around such a collection though.
  • Write your own iterator block as per Botz3000's answer
  • Use Enumerable.Repeat(item, 1) from LINQ, if you're using .NET 3.5.

The best answer here depends on the usage. If you only need this to call another method which uses a sequence, and you know it won't be modified, I'd probably use an array. For example, in order to call Concat on some other sequence, you might want:

var wholeList = regularList.Concat(new[] { finalValue });

I have confidence that Concat isn't going to mutate the array, and nothing else will ever see the reference to the array itself.

If you need to return the sequence to some other code, and you don't know what it might do with it, I'd probably use Enumerable.Repeat.

Solution 2

you could do this:

public IEnumerable<Foo> GetSingleFooAsEnumerable() { 
    yield return singleFoo;
}

Solution 3

The best idiomatic way to do this is something like new[] { foo } which just creates a 1-element array of whatever type foo is declared to be.

The one possible downside to this is that arrays aren't immutable, so somebody can cast your IEnumerable<T> to a T[] and change the value in there. This is fairly unlikely, though, so I don't worry about it.

Share:
12,414

Related videos on Youtube

Adibe7
Author by

Adibe7

Updated on May 17, 2022

Comments

  • Adibe7
    Adibe7 almost 2 years

    I have in instance of class foo and i want to return it as IEnumerable. Can i do it without creating a new list etc..

    Perhaps something like the following:

    IEnumerable<foo>.fromInstance(foo)
    
  • Konstantin Oznobihin
    Konstantin Oznobihin over 13 years
    There could be lots of reasons to turn single object into IEnumerable. E.g. one might want to pass it to a method which expects IEnumerable, or it could be used for emulating Maybe monad etc.
  • Konstantin Oznobihin
    Konstantin Oznobihin over 13 years
    one more option for those who use Rx (msdn.microsoft.com/en-us/devlabs/ee794896) is the EnumerableEx.Return method.
  • data
    data almost 11 years
    Short, concise, clean. This should be on top and accepted. +1
  • data
    data almost 11 years
    Missing the most clean way: new [] { item }
  • Jon Skeet
    Jon Skeet almost 11 years
    @data_smith: How is that not covered by "Create an instance of a collection class, like an array or a list"?
  • vincent gravitas
    vincent gravitas almost 11 years
    I second this response; this technique is also useful for enumerating over specific member variables.
  • data
    data almost 11 years
    @JonSkeet: Because it's kind of a catch-all phrase (your first item on the list). The new[] { item } that was suggested below should be right in the answer, IMHO. I personally did not know VS can infer the array type. But nvm, it's all just an opinion, i just think new[]{} is just more straightforward.
  • Adrian Ratnapala
    Adrian Ratnapala over 9 years
    If the method you call takes IEnumerable<T> as an argument, then isn't it safe to assume it doesn't mutate the sequence? At least not mutate it in any sense that will make it unsafe to send an array in?
  • Jon Skeet
    Jon Skeet over 9 years
    @AdrianRatnapala: Not necessarily. It could cast the parameter to an array (conditionally) and mutate it. It really depends on how much you trust the code you're talking to.
  • Marc L.
    Marc L. almost 3 years
    This is technically correct, so avoids my downvote. But given the other answers here, it looks almost... pessimal.