Skip to content

Allow yield return in lock #43988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ f1_keywords:
- "CS4013"
- "CS8154"
- "CS8176"
- "CS9237"
- "CS9238"
- "CS9239"
helpviewer_keywords:
Expand All @@ -28,7 +27,6 @@ helpviewer_keywords:
- "CS4013"
- "CS8154"
- "CS8176"
- "CS9237"
- "CS9238"
- "CS9239"
ms.date: 07/02/2024
Expand All @@ -51,7 +49,6 @@ That's by design. The text closely matches the text of the compiler error / warn
- [**CS4013**](#ref-safety-in-iterator-methods): *Instance of type cannot be used inside a nested function, query expression, iterator block or async method*
- [**CS8154**](#structure-of-an-iterator-method): *The body cannot be an iterator block because it returns by reference*
- [**CS8176**](#ref-safety-in-iterator-methods): *Iterators cannot have by-reference locals*
- [**CS9237**](#restrictions-on-iterator-methods): *'yield return' should not be used in the body of a lock statement*
- [**CS9238**](#restrictions-on-iterator-methods): *Cannot use 'yield return' in an 'unsafe' block*
- [**CS9239**](#restrictions-on-iterator-methods): *The `&` operator cannot be used on parameters or local variables in iterator methods.*

Expand Down Expand Up @@ -82,15 +79,13 @@ The body of an iterator method must conform to restrictions on the `yield return
- **CS1626**: *Cannot yield a value in the body of a try block with a catch clause*
- **CS1631**: *Cannot yield a value in the body of a catch clause*
- **CS1629**: *Unsafe code may not appear in iterators*
- **CS9237**: *''yield return' should not be used in the body of a lock statement*
- **CS9238**: *Cannot use 'yield return' in an 'unsafe' block*
- **CS9239**: *The `&` operator cannot be used on parameters or local variables in iterator methods.*

These errors indicate that your code violates safety rules because an iterator returns an element and resumes to generate the next element:

- You can't `yield return` from a `catch` or `finally` clause.
- You can't `yield return` from a `try` block with a catch clause.
- You can't `yield return` from inside a `lock` statement block. Doing so can cause deadlocks.
- You can't `yield return` from an `unsafe` block. The context for an iterator creates a nested `safe` block within the enclosing `unsafe` block.
- You can't use the `&` operator to take the address of a variable in an iterator method.

Expand Down
Loading