Hello,

I was testing the new .Net 5 framework and the new C# 9 and by coincidence I came across the following situation:

If I run the following code I get and InvalidCastException with the message “Unable to cast object of type ‘System.Double’ to type ‘System.Int32’.”

object a = 10d; Console.WriteLine((int)a); 

But when I run the following code it works:

object a = 10d; Console.WriteLine((int)(double)a); 

I was wondering the reason why this happens since in both cases I am performing the same cast.

Thanks!

submitted by /u/DanielAPO
[link] [comments]
source