Allow 'yield break' in expression-bodied iterator method #5402
-
I'm sure there's probably a good reason behind the scenes for this idiosyncrasy, but it feels like the following code should be valid, yet it does not compile: public IEnumerable<IAccessEntry> GetAccessEntries() => yield break; (returns When implementing interfaces you sometimes need to return an empty/dummy IEnumerable, so I've run across this a few times over the last weeks mocking out some of my components. Impact is obviously low, but feels inconsistent/unintuitive. I'm using C# 10 on .NET 6 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Why is this needed when you can return Or use a regular statement body, but write it on a single line? public IEnumerable<IAccessEntry> GetAccessEntries() { yield break; } |
Beta Was this translation helpful? Give feedback.
Why is this needed when you can return
Enumerable.Empty<T>()
instead? (Though you have to repeatT
there.)Or use a regular statement body, but write it on a single line?