Skip to content

Fix nullable event warning in interface events documentation #47684

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
Aug 4, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 31, 2025

The code snippet in the "How to implement interface events" documentation was generating a CS8618 nullable reference type warning when compiled with C# 8.0+ and nullable reference types enabled.

The issue occurred because the event was declared as non-nullable:

public event EventHandler ShapeChanged;

With nullable reference types enabled, the compiler warns that this event field might be null when exiting the constructor, since events are essentially multicast delegates that default to null.

This PR fixes the warning by:

  1. Making the event nullable - Changed EventHandler to EventHandler? in both the inline code snippet in the markdown documentation and the corresponding code file
  2. Modernizing the event invocation - Updated from explicit null checking to the null-conditional operator (ShapeChanged?.Invoke(this, e))
  3. Fixing character encoding - Corrected an ellipsis character encoding issue

The changes are minimal and maintain full backward compatibility while eliminating the nullable reference type warning that would appear when developers copy and use this code in projects with nullable reference types enabled.

Fixes #29880.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


Internal previews

📄 File 🔗 Preview link
docs/csharp/programming-guide/events/how-to-implement-interface-events.md docs/csharp/programming-guide/events/how-to-implement-interface-events

@dotnet-policy-service dotnet-policy-service bot added the okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings. label Jul 31, 2025
Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Code snippet gives compile warning about nullable event Fix nullable event warning in interface events documentation Jul 31, 2025
@Copilot Copilot AI requested a review from BillWagner July 31, 2025 16:06
Copilot finished work on behalf of BillWagner July 31, 2025 16:06
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

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

This is ready for final review.

@BillWagner BillWagner marked this pull request as ready for review August 1, 2025 18:59
@BillWagner BillWagner requested review from IEvangelist and a team as code owners August 1, 2025 18:59
@BillWagner BillWagner requested a review from tdykstra August 1, 2025 18:59
@BillWagner BillWagner merged commit 190605b into main Aug 4, 2025
14 checks passed
@BillWagner BillWagner deleted the copilot/fix-29880 branch August 4, 2025 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-csharp/svc fundamentals/subsvc okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Code snippet gives compile warning about nullable event
3 participants