Why doesn\'t the C# compiler tell me that this piece of code is invalid?
class Program
{
static void Main(string[] args)
{
dynamic d = 1;
Overload resolution is dynamic here. Visible in this code snippet:
class Program {
public static void Main() {
dynamic d = 1.0;
MyMethod(d);
}
public void MyMethod(int i) {
Console.WriteLine("int");
}
public static void MyMethod(double d) {
Console.WriteLine("double");
}
}
Works fine. Now assign 1 to d and note the runtime failure. The compiler cannot reasonably emulate dynamic overload resolution at compile time, so it doesn't try.