Budda
Budda

Reputation: 18353

How to compare enum and int values?

enum MyEnum
{
    Invalid=0,
    Value1=1,
    Value1=2,
}

void main ()
{
    MyEnum e1 = MyEnum.Value1;
    int i1 = 2;

    // Is there any difference how to compare enumEration values with integers?
    if ( e1==(MyEnum)i1 )... // 1st

    if ( (int)e1==i1 )... // 2nd

In each of mentioned cases we have convertion of enum to int or int to enum.

Is there any difference in these conversions (performance, any other)? Or they are exactly the same?

Thanks.

P.S. In current example I compare to 'magic number' but in real application I am getting data from integer field from DB.

Upvotes: 64

Views: 138726

Answers (7)

Carlos Ripoll
Carlos Ripoll

Reputation: 11

Maybe you could just declare your enum as a static class with all of the possible values being static ints

public static class myEnum{
public static int myInt = 1;
public static int myInt2 = 2;
}

Then compare them directly
int myComparedVar = 2;
if(myComparedVar == myEnum.myInt){...}

Upvotes: 1

Vivek kumar
Vivek kumar

Reputation: 289

This can help.

var constant = 1;
if(constant == (int)MyEnum.Valid1){
......
}

Upvotes: 24

David Rogers
David Rogers

Reputation: 4280

They are exactly the same. Displaying the generated IL using the Debug, Windows, Disassembly (Ctrl-Alt-D) gives you:

MyEnum e1 = MyEnum.Value1;
00260834  mov         dword ptr [ebp-3Ch],1  
int i1 = 2;
0026083B  mov         dword ptr [ebp-40h],2  

// Is there any difference how to compare enumEration values with integers?
if (e1 == (MyEnum) i1) 
00260842  mov         eax,dword ptr [ebp-3Ch]  
00260845  cmp         eax,dword ptr [ebp-40h]  
00260848  sete        al  
0026084B  movzx       eax,al  
0026084E  mov         dword ptr [ebp-44h],eax  
00260851  cmp         dword ptr [ebp-44h],0  
00260855  je          00260858  
; // 1st
00260857  nop  

if ((int)e1 == i1)
00260858  mov         eax,dword ptr [ebp-3Ch]  
0026085B  cmp         eax,dword ptr [ebp-40h]  
0026085E  sete        al  
00260861  movzx       eax,al  
00260864  mov         dword ptr [ebp-48h],eax  
00260867  cmp         dword ptr [ebp-48h],0  
0026086B  je          0026086E  
; // 2nd
0026086D  nop  

Upvotes: 7

Sam Trost
Sam Trost

Reputation: 2173

I would recommend casting the int to the representative enum value when you read it from the database. This will greatly improve the readability of your code.

enum MyEnum
{
    Invalid=0,
    Value1=1,
    Value1=2,
}

MyEnum dbValue = ReadEnumFromDB();
if(dbValue == MyEnum.Invalid)
{
   ...
}

Upvotes: 8

Matthew Whited
Matthew Whited

Reputation: 22443

Enumerations in .Net are really just pretty meta-structures over the base integral type. (By default that type is int.) If you look at the Generated IL for an enumeration you will find it is really a standard type with several static fields for each of the particular enumeration elements. As such the enum can be cast between integral types transparently.

Related answer

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 191058

I would go with the 2nd method. To me, it makes more logical sense. It would eliminate runtime exceptions if i2 is out of range.

Upvotes: 4

Melllvar
Melllvar

Reputation: 2086

It doesn't matter which you use, they will perform identically. If there is no enum for an integer value, .net creates one at runtime. There can be no exceptions.

However, Xichen Li is correct - why would you want to compare an enum against an integer value?

Upvotes: 16

Related Questions