Skip to content

Unexpected pattern #837

Answered by viceroypenguin
thomashilke asked this question in Q&A
Mar 30, 2022 · 3 comments · 12 replies
Discussion options

You must be logged in to vote

This is a pattern you will find even in the .net runtime in the enumerable methods. The most important thing to realize is that if you do a yield return, none of the code in the method will run until the first time the consumer actually enumerates the data. This means that arguments are not checked immediately in the normal method. Doing the local method pattern allows for immediate argument validation while still using the yield return pattern.

Example consumer code:

var data = source.Pairwise(null);

// Do something else

return data;

Under the default version (what you provided), then the null argument will not be checked in this method, but will be checked when the caller tries to use…

Replies: 3 comments 12 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
11 replies
@thomashilke
Comment options

@i3arnon
Comment options

@viceroypenguin
Comment options

@thomashilke
Comment options

@atifaziz
Comment options

Answer selected by thomashilke
Comment options

You must be logged in to vote
1 reply
@atifaziz
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants