Fix nullable event warning in interface events documentation #47684
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.
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:
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:
EventHandler
toEventHandler?
in both the inline code snippet in the markdown documentation and the corresponding code fileShapeChanged?.Invoke(this, e)
)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