Smartis Smartis - 2 months ago 9
C# Question

C# Comparing Values from dynamic's

Today i tried an old project, in my company and got an error which makes me curious. The issue line looks something like this:

if((dynamic)com_list.GetIntValue() != (dynamic)container.GetEnumValue())


The exception shows clearly that you can't compare
Int32
with an
Enum
.

But i wonder, could this have ever worked, in some circumstance?

Are there changes in the
dynamic
Keyword which don't allow this anymore?




BTW, he also build this in the code like this:

if((dynamic)com_list.GetIntValue() != (dynamic)container.GetBooleanValue())





I'm still confused, why somebody would put this kind of comparing into productiv code.

Answer

No. The dynamic specification hasn't changed and I am pretty sure the evaluation in the compiler of such a trivial comparison didn't change overnight in one release to another. Most likely that code never worked.

Without additional cast from enum to int (or the other way around) it won't work.