Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 5, 2025

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 and const zero values to any enum type, even when the enum has no member with that value. This can lead to unexpected behavior:

public enum GpioPort
{
    GpioA = 1,
    GpioB,
    GpioC,
    GpioD
}

// Both of these compile without warning but create invalid enum values
var port1 = GetValue(0);        // Implicit conversion from 0
const int X = 0;
GpioPort port2 = GetValue(X);   // Implicit conversion from const 0

Solution

Added a new "Implicit conversions from zero" section that:

  1. Explains the behavior - Documents how zero conversions work and why they can be problematic
  2. Provides concrete examples - Shows the exact scenarios reported in the issue using a GpioPort enum
  3. Demonstrates validation - Shows how to use Enum.IsDefined() to check for valid enum values
  4. Offers best practices - Three specific recommendations:
    • Consider defining a zero member when appropriate
    • Use Enum.IsDefined() for validation when converting from numeric types
    • Be cautious with numeric parameters that might be implicitly converted

The 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

📄 File 🔗 Preview link
docs/csharp/language-reference/builtin-types/enum.md docs/csharp/language-reference/builtin-types/enum

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Potential dangers with enum Document enum zero conversion dangers and provide validation guidance Aug 5, 2025
@Copilot Copilot AI requested a review from BillWagner August 5, 2025 17:46
Copilot finished work on behalf of BillWagner August 5, 2025 17:46
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 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:
Copy link
Member

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.

Copy link
Contributor Author

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)

@BillWagner
Copy link
Member

@copilot address the review comments

…recommendation

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Aug 6, 2025

@copilot address the review comments

All review comments have been addressed in commit 0a6a359:

  • Updated code snippet to use ::: syntax
  • Corrected explanation about why zero conversion exists (0 bit pattern default)
  • Strengthened recommendation to "almost always define a member with value 0"

@Copilot Copilot AI requested a review from BillWagner August 6, 2025 18:19
Copilot finished work on behalf of BillWagner August 6, 2025 18:19
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 6, 2025 18:43
@BillWagner BillWagner requested a review from a team as a code owner August 6, 2025 18:43
@BillWagner BillWagner requested a review from adegeo August 6, 2025 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Potential dangers with enum
2 participants