Skip to content

Add examples and diagnostics guidance for static anonymous functions (Fixes #25499) - #56663

Open
Manish-1kumar wants to merge 2 commits into
dotnet:mainfrom
Manish-1kumar:users/manish/issue-25499-static-anonymous-functions-examples
Open

Manish-1kumar wants to merge 2 commits into
dotnet:mainfrom
Manish-1kumar:users/manish/issue-25499-static-anonymous-functions-examples

Conversation

@Manish-1kumar

@Manish-1kumar Manish-1kumar commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #25499

Summary of Changes

  • Expands the static lambda documentation in \docs/csharp/language-reference/operators/lambda-expressions.md\ into a dedicated Static anonymous functions section.
  • Adds compilable code snippets in \GeneralExamples.cs\ showing:
    • Static lambda expression.
    • Static anonymous method using the \delegate\ operator (\static delegate (int a, int b) { ... }).
    • Static lambda accessing constants and static members without allocating closures.
  • Documents related compiler diagnostics (CS8828 and CS8829) when attempting to capture local variables or \ his\ from enclosing scopes.
  • Explains the performance advantage of static anonymous functions in preventing heap allocations on critical code paths.

Internal previews

File Preview link
docs/csharp/language-reference/operators/lambda-expressions.md Learn preview
docs/csharp/language-reference/operators/snippets/lambda-expressions/GeneralExamples.cs Learn preview

Build report

@dotnetrepoman dotnetrepoman Bot added this to the November 2026 milestone Nov 1, 2026
@dotnet-policy-service dotnet-policy-service Bot added dotnet-csharp/svc lang-reference/subsvc community-contribution Indicates PR is created by someone from the .NET community. labels Nov 1, 2026
@BillWagner
BillWagner requested a balanced review from Copilot November 25, 2026 16:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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
Manish-1kumar force-pushed the users/manish/issue-25499-static-anonymous-functions-examples branch from 4dd91ef to 89f105e Compare November 30, 2026 17:40

@BillWagner BillWagner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Manish-1kumar

I have one small clarification that will help readers understand this topic. Once that's in place, I'll :shipit:

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`):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Indicates PR is created by someone from the .NET community. dotnet-csharp/svc lang-reference/subsvc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

static anonymous functions examples

3 participants