-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add Enum.SetFlag and Enum.UnsetFlag #50067
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
@mburbea I think the comment you posted over at #14084 (comment) with this API suggestion is fine. We can keep iterating on those old threads with new ideas and bring it back for API review. |
Not necessarily, depending on how it is implemented. For example, we could make use of an internal Of course, the above would only be possible for the generic version. The |
From my testing, it seemed that the version that shows up in the method looks worse than when it is inlined at the callsite. The .net jit does a very good job with it if it can inline and creates nice and clean assembly. The diffs: https://www.diffchecker.com/MWqgZmhw @GrabYourPitchforks, if that's the case we can close this issue. |
Dupe of #14084 |
Background and Motivation
Note: issue #14084, proposed this earlier but that api needed work and didn't follow the proper api-request format.
Flagged enum's have a friendly api to check for the existence of a flag, but don't currently have a counter part to set or unset a flag. I propose adding one to make it easier to work with flagged enums without resorting to writing bit manipulating expressions. When dealing with generic enums, where bit manipulation is unavailable, you'd have to write unsafe code.
Proposed API
Note that since this interface would appear on all enum's, this API must throw
InvalidOperationException
if the instance does not match the type ofT
. I do not believe we should throw for enum's that do not have aFlagsAttribute
.Usage Examples
Here is an actual example from this code I wrote recently.
Alternative Designs
The code can currently be accomplished as a one liner function for a given enum
E
.Or as an ugly extension method that works generically. . Unfortunately, the ugly extension method does produce some extra noise at the moment.
Also, since technically, nothing is preventing this from working on bitmasks, the methods could be renamed to
Enum.SetMask
Risks
For this to work well, it will likely need to be an intrinsic like
Enum.HasFlag
.The text was updated successfully, but these errors were encountered: