Skip to content

Implement IEquatable for StringEnum #739

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 4 commits into
base: main
Choose a base branch
from
Open

Conversation

JamieMagee
Copy link
Member

Resolves #738


Before the change?

After the change?

Pull request checklist

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)

Does this introduce a breaking change?

Please see our docs on breaking changes to help!

  • Yes
  • No

Copy link

👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labeled with Status: Up for grabs. You & others like you are the reason all of this works! So thank you & happy coding! 🚀

kfcampbell
kfcampbell previously approved these changes Aug 15, 2025
JamieMagee and others added 3 commits August 15, 2025 16:20
- case insensitivity
- `ToString()` implementation
- override `==` and `!=` operators
@JamieMagee JamieMagee force-pushed the stringenum-iequatable branch from 8a4964e to 593146a Compare August 15, 2025 23:20
}

public override string ToString() => this.StringValue;

private static ArgumentException GetArgumentException(string? value) => new(string.Format(

Choose a reason for hiding this comment

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

This could be turned into a throw helper with a DoesNotReturn attribute

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, I'm not quite sure what you mean here.

Choose a reason for hiding this comment

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

Something like #742

@danielmarbach
Copy link

danielmarbach commented Aug 18, 2025

Looking at ToEnum and ToEnumString only being used in specific cases now, I wonder whether the throwing in there is still needed. For example ToEnumString is only used in

        if (!Enum.IsDefined(typeof(TEnum), parsedValue))
        {
            ThrowArgumentException(parsedValue.ToString());
        }

        this.StringValue = ToEnumString(parsedValue);

which already covers the enum ranges possible, and if the StringValue is null the .Value property will throw too.

TryParseEnum is the only method relying on ToEnum which now requires a try / catch due to the argument exception being thrown but only ever sets the value to null and returns false. Why not simply return a nullable value from ToEnum instead of throwing?

Sorry these are only superficial reviews and the sort of come in piece by piece because I have limited time available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🆕 Triage
Development

Successfully merging this pull request may close these issues.

[BUG]: StringEnum equality comparison is suprising
3 participants