-
Notifications
You must be signed in to change notification settings - Fork 6k
Document enum zero conversion dangers and provide validation guidance #47737
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great start, but I have a few comments.
|
||
In the preceding example, both `port1` and `port2` are assigned the value `0`, but `GpioPort` has no member with that value. The <xref:System.Enum.IsDefined%2A?displayProperty=nameWithType> method confirms these are invalid enum values. | ||
|
||
This implicit conversion exists for backward compatibility, but it can introduce bugs in your code. To avoid these issues: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Backwards compatibility isn't the motivation for this rule. The reason 0 is always valid is because the 0 bit pattern is the default for all struct types, including all enum types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected the explanation - it's because the 0 bit pattern is the default for all struct types, not backward compatibility. (0a6a359)
@copilot address the review comments |
…recommendation Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
There was a problem hiding this 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.
This PR addresses a documentation gap regarding the implicit conversion behavior of zero values to enum types in C#. The current documentation briefly mentions that
(E)0
is the default value even when zero doesn't have a corresponding enum member, but doesn't adequately warn developers about the potential risks.Problem
C# allows implicit conversions from the literal value
0
andconst
zero values to any enum type, even when the enum has no member with that value. This can lead to unexpected behavior:Solution
Added a new "Implicit conversions from zero" section that:
GpioPort
enumEnum.IsDefined()
to check for valid enum valuesEnum.IsDefined()
for validation when converting from numeric typesThe code examples compile and run correctly, demonstrating both the problematic behavior and the safer validation approach.
Fixes #39809.
💡 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