简体   繁体   中英

Compare nullable enum-values within QuickWatch works while running app throws exception

I have a simple enumeration:

enum MyEnum { First = 1 }

I use this enum in the following code:

object i = 1;
MyEnum expected = MyEnum.First;         
if ((MyEnum?)i == expected) { }

Now when running this code I get an InvalidCastException . However when I enter this if-statement into quickwatch from VS I get the expected value of First .

The object i comes from a databasde-query and can therefor also be DBNull . In this case I set it explicitely to null whereas I have to cast to MyEnum? instead of MyEnum .

What can I do to compare the nullable enum-value to my expected one?

Strange solution: first casting to int and then to the enum works, obviously we have to unbox the object first in order to convert it to the nullable enum.

if ((MyEnum?)(int) i == expected)

However I still don´t get why quickwatch is able to do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM