Search code examples
c#enumerable

How to return empty IEnumerable from expression-bodied member


Imagine you have to implement interface required property:

IEnumerable<int> Ids { get; }

which (implemented) returns empty enumeration. You may write:

public IEnumerable<int> Ids
{
    get { yield break; }
}

But is there a way how to use expression bodied member syntax, so you may have this on single line?


Solution

  • Use the Enumerable.Empty<type> function.

    IEnumerable<int> Ids => Enumerable.Empty<int>();