Skip to content

Commit 3c8b798

Browse files
committed
Add stubs for new warnings and errors.
1 parent d7a4fab commit 3c8b798

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: Errors and warnings for iterator methods and `yield return`
3+
description: Use article to diagnose and correct compiler errors and warnings when you write iterator methods that use `yield return` to enumerate a sequence of elements.
4+
f1_keywords:
5+
- "CS9237"
6+
- "CS9238"
7+
- "CS9239"
8+
helpviewer_keywords:
9+
- "CS9237"
10+
- "CS9238"
11+
- "CS9239"
12+
ms.date: 07/02/2024
13+
---
14+
# Errors and warnings related to the `yield return` statement and iterator methods
15+
16+
There are numerous *errors* related to the `yield return` statement and iterator methods:
17+
18+
<!-- The text in this list generates issues for Acrolinx, because they don't use contractions.
19+
That's by design. The text closely matches the text of the compiler error / warning for SEO purposes.
20+
-->
21+
- [**CS9237**](#): *'yield return' should not be used in the body of a lock statement*
22+
- [**CS9238**](#): *Cannot use 'yield return' in an 'unsafe' block*
23+
- [**CS9239**](#): *The `&` operator cannot be used on parameters or local variables in iterator methods.*
24+
25+
In addition, the compiler might produce the following *warning* related to `lock` statements and thread synchronization:
26+
27+
- [**CS9216**](#lock-warning): *A value of type `System.Threading.Lock` converted to a different type will use likely unintended monitor-based locking in `lock` statement.*
28+
29+
## New errors
30+
31+
- **CS9237**: *''yield return' should not be used in the body of a lock statement*
32+
- **CS9238**: *Cannot use 'yield return' in an 'unsafe' block*
33+
- **CS9239**: *The `&` operator cannot be used on parameters or local variables in iterator methods.*
34+
35+
These errors indicate that your code violates safety rules because an iterator returns an element and resumes to generate the next element:
36+
37+
- You can't `yield return` from inside a `lock` statement block. Doing so can cause deadlocks.
38+
- You can't `yield return` from an `unsafe` block. The context for an iterator creates a nested `safe` block within the enclosing `unsafe` block.
39+
- You can't use the `&` operator to take the address of a variable in an iterator method.
40+
41+
You must update your code to remove the constructs that aren't allowed.

docs/csharp/language-reference/toc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,9 @@ items:
464464
CS8351, CS8373, CS8374, CS8388, CS8977, CS9072, CS9077, CS9078, CS9079, CS9085, CS9086, CS9087, CS9089, CS9091, CS9092,
465465
CS9093, CS9094, CS9095, CS9096, CS9097, CS9101, CS9102, CS9104, CS9190, CS9191, CS9192, CS9193, CS9195, CS9196, CS9197,
466466
CS9198, CS9199, CS9200, CS9201
467+
- name: Iterator methods
468+
href: ./iterator-yield.md
469+
displayName: yield return, yield break, CS9237, CS9238, CS9239
467470
- name: Params modifier
468471
href: ./compiler-messages/params-arrays.md
469472
displayName: >

0 commit comments

Comments
 (0)