Comparing `enum` and `int`
See original GitHub issueDescription
Before #1202 we had an asymmetry in how we compared an enum
and an int
, as it worked in one direction, but not the other.
enum MyEnum
{
One = 1
}
[Fact]
public void when_comparing_an_enum_to_a_numeric_it_should_TODO()
{
MyEnum.One.As<object>().Should().Be(1);
// passes
}
[Fact]
public void when_comparing_a_numeric_to_an_enum_it_should_TODO()
{
1.As<object>().Should().Be(MyEnum.One);
// fails
}
After #1202, none of them passes.
There were no tests covering these cases.
Should we allow comparing enum
s with numeric types?
Issue Analytics
- State:
- Created 4 years ago
- Comments:14 (10 by maintainers)
Top Results From Across the Web
How to compare enum and int values?
This can help. var constant = 1; if(constant == (int)MyEnum.Valid1){ ...... } ... It doesn't matter which you use, they will perform identically....
Read more >C# – How to compare enum and int values
It doesn't matter which you use, they will perform identically. If there is no enum for an integer value, .net creates one at...
Read more >How to compare Enum values in C#? - GeeksforGeeks
Enum.CompareTo(Object) Method is used to compare the current instance to a specified object and returns an indication of their relative values.
Read more >How to get int value from enum in C# - Dofactory
A C# code example that shows how to use type casting to convert an enum to an int value. Be aware that enums...
Read more >52763 – Warning if compare between enum and non-enum type
Hi, the following program doesn't create a warning. It would be nice to get an warning each time a enumeration type is compared...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Be careful when mentioning integers, integers are only 1 of the 8 types that can back an enum.
Logged as #2011