一直在使用4.0 DLR,并将动态与对象进行比较并遇到了这样的情况:
码:
object x = 10;
Console.WriteLine("x = {0} and is a {1}.\n", x, x.GetType());
x = (int)x + 3;
Console.WriteLine("x = {0} and is a {1}.\n", x, x.GetType());
x = x + "a";
Console.WriteLine("x = {0} and is a {1}.\n", x, x.GetType());
Run Code Online (Sandbox Code Playgroud)
结果:
x = 10并且是System.Int32.
x = 13并且是System.Int32.
x = 13a并且是System.String.
对我来说,看起来对象试图在运行时(动态)使对象适合类型.但是,如果我没有将x转换为第3行的int,它会给我一个编译器区域,这对于静态类型来说似乎是正确的.但是它允许我向x添加"a",现在它将其识别为字符串.
我错过了什么?