|
by Eric Lippert via Fabulous Adventures In Coding on 5/27/2010 1:55:00 PM
Another interesting question from StackOverflow. Consider the following unfortunate situation:
object result;bool isDecimal = GetAmount(out result);decimal amount = (decimal)(isDecimal ? result : 0);
The developer who wrote this code was quite surprised to discover that it compiles and then throws “invalid cast exception” if the alternative branch is taken.
Anyone see why?
In regular algebra, multiplication is “distributive” over addition. That is q * (r + s) is the same as q * r + q * s. The
... [ read more ]
|