Usually VB is the more verbose of the two languages, so I’m surprised to see C# being more verbose in this scenario! Why can’t I just yield 42;
in C# like I can in VB? What’s the purpose of the return
? yield
is not used in any other capacity in C# that it would be ambiguous, is it?
So that the introduction of iterators was a non-breaking change.
in C# 1.0 “yield” was a valid identifier. In order to prevent iterators from breaking existing code, yield was made a contextual keyword instead of a reserved word, such that it only had special meaning with other keywords (return and break).
Yield can be used with ‘return’ or ‘break’.
But break
isn’t a valid value to return from a function, so it would be equally unambiguous to say yield 42;
and still keep yield break;
.
C# devs
null reference exceptions