question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Comparing `enum` and `int`

See original GitHub issue

Description

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 enums with numeric types?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
eNeRGy164commented, Dec 13, 2019

Enums are in fact backed by integers, so my first thought is yes, this should be possible.

Be careful when mentioning integers, integers are only 1 of the 8 types that can back an enum.

An enumeration type has an underlying type, which can be any integral numeric type and is int by default. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/enum

1reaction
fakhrulhilalcommented, Oct 12, 2022

Hmm, it could be a bug where auto-conversion and property mapping don’t work together. Can you file a new bug instead?

Logged as #2011

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found