What is the being called here: return _()
The _()
here is a call to the local function called _
. Unusual, but valid.
A local function is broadly like a regular method, except that it can only be called by name (i.e. the usual way you call a method) from inside the method that declares it (as Eric points out in a comment, there are some other ways that it could be called, for example via a delegate passed out from the method), and (unless decorated static
) it can pick up locals and parameters from the declaring method as state.
In this case, the intent is to perform eager parameter validation.
With validation code in the iterator block, the parameters validation would be deferred until the first MoveNext()
call. (i.e. it wouldn't complain about source
being null
until someone attempts to foreach
over the data).
IEnumerable<TResult> _() {}
is local function that called in return _();