This code is compiled.
using System.IO;
using System;
class Program
{
static void Main()
{
object obj = 0;
long x = (long) obj;
Console.WriteLine(x);
}
}
In run-time it throws this Unhandled Exception:
System.InvalidCastException: Cannot cast from source type to destination type.
at Program.Main () [0x00000] in :0
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidCastException: Cannot cast from source type to destination type.
at Program.Main () [0x00000] in :0
The following chunk is compiled and run correctly.
static void Main()
{
object obj = 0;
long x = (int) obj;
Console.WriteLine(x);
}
> 0
What's going on?