C# object looks like dynamic type

.net, c#, dynamic

Solution

You are missing the fact that the `+` operator, when applied to strings, does an automatic conversion (by calling the `.ToString()` method on the operand that is not an instance of the `String` type).

Problem

Have been playing around with the 4.0 DLR and was comparing dynamic to object and came across this: Code: ``` 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()); ``` Result: x = 10 and is a System.Int32. x = 13 and is a System.Int32. x = 13a and is a System.String. To me, it looks like object tries to fit the object to a type at runtime (dynamic). However if I don't cast x to an int on the 3rd line, it gives me a compiler area which seems correct for static typing. But then it allows me to add an "a" to x and now it recognizes it as a string. What am I missing?

Original source