15

In some case I've to return an empty list of items in a method. Most of the case, I'm returning an IEnumerable<T>, so the Enumerable.Empty<T>() does exactly the job.

But I've one case where I've to return absolutely an IList<T> instance(we could use an IEnumerable, but this would results in a code much less efficient).

I didn't found an equivalent for the IList. I can perfectly imagine doing my own Singleton provider for empty list, but I would like to know if there is something I did miss?

Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
J4N
  • 19,480
  • 39
  • 187
  • 340
  • 1
    why do you say code would be less efficient ? – Vignesh.N Oct 06 '16 at 08:59
  • 2
    Check this solution: http://stackoverflow.com/a/6802982/6469077 – Nico Oct 06 '16 at 08:59
  • 3
    return new List() – Tim Schmelter Oct 06 '16 at 09:03
  • @Vignesh.N Because I will normally have a lot of items, and I will have to remove some of them in the list, so it means that I will have to create a List from the IEnumerable to start to works with. – J4N Oct 06 '16 at 10:42
  • @heinzbeinz and @TimSchmelter: The goal is not have to create this object on each case but to have an empty object that could be reused, like the `Enumerable.Empty()`. – J4N Oct 06 '16 at 10:44

1 Answers1

26

As Array is implementing IList use

Array.Empty<T>()
Rafal
  • 12,391
  • 32
  • 54