The local variable can be called profitability.

Just found out

foreach (int yield in items) {
  yield return yield * 2;
}

or

int yield = 10; 

are valid codes in C #.

I can understand that it would be easy for the compiler to distinguish between variable returns and iterator output, but still this adds to the confusion and reduces the readability of the code.

Do we know the exact reasons why this is allowed?

+5
source share
2 answers

Do we know the exact reasons why this is allowed?

Yes. yield- This is just a contextual keyword. It was introduced in C # 2, and the team did not want to break the code that it already used yieldas an identifier.

The same is true for other contextual keywords that were entered with 1.0, for example partial. See Eric Lippert's blog post for a complete list.

, ? , , , . :

private int _;
private string __;
// etc

? , , yield ?

+18

yield - . , .

. , , .

, yield :

bool yield = false;
// bla bla

, ? , .

+2

All Articles