Repository navigation
Add examples and diagnostics guidance for static anonymous functions (Fixes #25499) - #56663
Open
Manish-1kumar wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The lambda-expression documentation lists incorrect capture diagnostic IDs; it should use CS8820 and CS8821 and link to the existing resolution guidance.
1 open issue
What changed in this PR
Expands the C# lambda reference with static anonymous function examples and guidance, addressing the examples requested in #25499.
Changes:
- Adds a dedicated section covering static lambdas, anonymous methods, capture restrictions, and closure allocations.
- Adds snippets for a static anonymous method and a static lambda that references a constant and a static member.
- Adds compiler diagnostic links and refreshes the article date.
| File | Description |
|---|---|
docs/csharp/language-reference/operators/snippets/lambda-expressions/GeneralExamples.cs |
Adds two static anonymous function examples. |
docs/csharp/language-reference/operators/lambda-expressions.md |
Expands static anonymous function guidance and includes the new snippets. |
Review effort: Balanced
Manish-1kumar
force-pushed
the
users/manish/issue-25499-static-anonymous-functions-examples
branch
from
November 30, 2026 17:40
4dd91ef to
89f105e
Compare
BillWagner
reviewed
Dec 1, 2026
BillWagner
left a comment
Member
There was a problem hiding this comment.
I have one small clarification that will help readers understand this topic. Once that's in place, I'll ![]()
| To prevent unintentional capture of local variables or instance state by the lambda, apply the `static` modifier to a lambda expression: | ||
| ### Static anonymous functions | ||
|
|
||
| Starting in C# 9, you can apply the `static` modifier to a lambda expression or [anonymous method](delegate-operator.md) to prevent the unintentional capture of local variables or instance state (`this`): |
Member
There was a problem hiding this comment.
Let's add that code captures instance state by capturing any field in this instance:
Suggested change
| Starting in C# 9, you can apply the `static` modifier to a lambda expression or [anonymous method](delegate-operator.md) to prevent the unintentional capture of local variables or instance state (`this`): | |
| Starting in C# 9, you can apply the `static` modifier to a lambda expression or [anonymous method](delegate-operator.md) to prevent the unintentional capture of local variables or instance state (`this`, or fields of `this` instance): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #25499
Summary of Changes
Internal previews
Build report