In C#, the type dynamic
allows you to change a variable\'s type at runtime, for example:
dynamic x = \"foo\";
x = 42;
Another exam
The type of the conditional operator is determined regardless of the surrounding expression. In other words it really doesn't matter what is before =
, (true) ? "foo" : 42;
is illegal.
The solution is to cast the types of the operands instead:
dynamic x = (true) ? (dynamic) "foo" : (dynamic) 42;
You can cast just one of them if you wish.
One other thing, the operator's name is "the conditional operator", not "ternary operator", even if it is the only ternary operator in C#.