2010-11-23 74 views
0

我有这个filetypeenum:我的枚举应该被接受为int?

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

为什么它say == cant be applied to int type and FileType当我尝试比较:

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

回答

4

因为转换不隐。 C#不会自动在枚举类型和枚举基类型之间进行转换,因为在很多情况下,这会导致程序员不期望的行为。

试试这个:

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

尝试铸造它

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

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

+1另一种方法,如果(类型==(INT)FileType.jpeg) – 2010-11-23 18:40:53

+0

就像我更新。 :) – hunter 2010-11-23 18:41:36