8

I often find myself writing sth. like this:

if (condition) { yield return whatever; yield break; } 

I find it quite verbose to have to use two yield statements for the standard paradigm "return one value and exit method". I know I can just return new List<type>() { whatever }; but that would defeat the benefit of using yield in the rest of the method.

Is there a more elegant way to yield return a value and exit the method?

1 Answer 1

4

This is probably your only other option.

if (condition) { yield return 1; } else { yield return 2; yield return 3; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Hm, since I like to exit early, this seems like no real alternative.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.