Jason94
Jason94

Reputation: 13610

My enums should be accepted as int?

I have this filetypeenum:

public enum FileType : int
{
    jpeg = 0,
    png = 1,
}

why does it say == cant be applied to int type and FileType when i try to compare:

int type = 1;
if( type == FileType.jpeg)

??

Upvotes: 0

Views: 98

Answers (2)

hunter
hunter

Reputation: 63522

Try casting it

if((FileType)type == FileType.jpeg)

or

if(type == (int)FileType.jpeg)

Upvotes: 5

cdhowie
cdhowie

Reputation: 169018

Because the conversion is not implicit. C# will not automatically convert between enum types and the enum base type because in many situations this can cause behavior not expected by the programmer.

Try this instead:

if ((FileType)type == FileType.jpeg)

Upvotes: 4

Related Questions