Consider the following code:
public class Foo1
{
public dynamic dowork()
{
return 10;
}
}
And in my Main
, I call like:
int i = new Foo1().dowork();
The return value is 10. My question is why no Unboxing
is required here?But in watch
I've verified the Return Type
of dowork
is System.Object
.
It is unboxing - but it's doing it implicitly. There's an implicit conversion from any dynamic
expression to any type. The exact conversion performed will depend on the execution-time type of the value.
From section 6.1.8 of the C# 5 specification:
An implicit dynamic conversion exists from an expression of type dynamic
to any type T
. The conversion is dynamically bound (§7.2.2), which means that an implicit conversion will be sought at run-time from the run-time type of the expression to T
. If no conversion is found, a run-time exception is thrown.
(There's a slight nuance here in that it's a conversion from any expression of type dynamic
rather than a conversion from the dynamic
type itself. That avoids some conversion loops which would cause issues elsewhere in the spec.)