How to return empty IEnumerable from expression-bodied member

2449 views c# enumerable
9

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?

answered question

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

1 Answer

8

Use the Enumerable.Empty<type> function.

public IEnumerable<int> Ids => Enumerable.Empty<int>

posted this

Have an answer?

JD

Please login first before posting an answer.