Smartis Smartis - 1 year ago 77
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
with an

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

Are there changes in the
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download