17

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)
Abel
  • 56,041
  • 24
  • 146
  • 247
Adibe7
  • 3,469
  • 7
  • 30
  • 36
  • 2
    possible duplicate of [Passing a single item as IEnumerable](http://stackoverflow.com/questions/1577822/passing-a-single-item-as-ienumerablet) – nawfal Feb 17 '13 at 12:19
  • Possible duplicate of [Passing a single item as IEnumerable](https://stackoverflow.com/questions/1577822/passing-a-single-item-as-ienumerablet) – Ian Ringrose Jun 22 '17 at 18:11

4 Answers4

21

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • one more option for those who use Rx (http://msdn.microsoft.com/en-us/devlabs/ee794896) is the EnumerableEx.Return method. – Konstantin Oznobihin Jan 24 '11 at 07:26
  • Missing the most clean way: `new [] { item }` – data May 07 '13 at 14:03
  • @data_smith: How is that not covered by "Create an instance of a collection class, like an array or a list"? – Jon Skeet May 07 '13 at 15:01
  • @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. – data May 20 '13 at 07:56
  • 1
    If the method you call takes `IEnumerable` 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? – Adrian Ratnapala Jan 20 '15 at 12:18
  • @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. – Jon Skeet Jan 20 '15 at 13:39
17

you could do this:

public IEnumerable<Foo> GetSingleFooAsEnumerable() { 
    yield return singleFoo;
}
Botz3000
  • 39,020
  • 8
  • 103
  • 127
14

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.

Gabe
  • 84,912
  • 12
  • 139
  • 238
0

IENumerable is supposed to be used for something that you can enumerate through, so using it for a single instance seems quite strange. If you really need to, you can get it done like this. It might be a better way, but this should get the job done:

List<foo> myList = new List<foo>();
myList.Add( myInstanceOfFoo );
IEnumerable myEnumerable = myList.AsEnumerable();

Regardless of how you see this, you are actually trying to make a list of one element, and then it's really no reason to make it a list.

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
  • 4
    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 Jan 24 '11 at 07:20
  • This is technically correct, so avoids my downvote. But given the other answers here, it looks almost... pessimal. – Marc L. Jun 03 '21 at 17:17