Consider the following code:
static void Main()
{
dynamic a = 1;
int b = OneMethod(a);
}
private static string OneMethod(int number)
{
return "";
}
请注意,
b的类型
和OneMethod的返回类型
不匹配。尽管如此,在运行时它仍然构建并抛出异常。 我的问题是为什么编译器允许这样? 或者说这背后的哲学是什么?这背后的原因可能是
编译器不知道调用哪个OneMethod,因为a是动态的。
但是为什么它不能看到只有一个OneMethod
呢? 这肯定会在运行时引发异常。