I’ve run into a bit on an Anomaly where for the first time ever, using the var keyword bit me.
Take this very simple method
public static Int32? GetNullableInt32(Int32 num)
{
return new Nullable<Int32>(num);
}
Now we can call this method with a dynamic parameter and everything will work as expected.
public static void WorksAsAdvertised()
{
dynamic thisIsAnInt32 = 42;
//Explicitly defined type (no problems)
Int32? shouldBeNullableInt32 = GetNullableInt32(thisIsAnInt32);
Console.Write(shouldBeNullableInt32.HasValue);
}
However, by declaring shouldBeNullableInt32 using implicit typing, the results are far from what I would expect.
public static void BlowsUpAtRuntime()
{
dynamic thisIsAnInt32 = 42;
//Now I'm a dynamic{int}... WTF!!!
var shouldBeNullableInt32 = GetNullableInt32(thisIsAnInt32);
//Throws a RuntimeBinderException
Console.Write(shouldBeNullableInt32.HasValue);
}
Instead of being a Nullable<Int32> the return value get’s treated as a dynamic type. And even then, the underlying Nullable<T> is not preserved. Since System.Int32 has no property named HasValue, a RuntimeBinderException is thrown.
I would be VERY curious to hear from someone who can actually explain what is happening (not just guess).
Two Questions
- Why does
shouldBeNullableInt32get implicitly typed as a dynamic when the return type ofGetNullableInt32clearly returns aNullable<Int32>? - Why is the underlying
Nullable<Int32>not preserved? Why adynamic{int}instead? (Answered here: C# 4: Dynamic and Nullable<>)
UPDATE
Both Rick Sladkey’s answer and Eric Lippert’s answer are equally valid. Please read them both 🙂
This is because while it is apparent to us that
GetNullableInt32is the method that is going to be called, because of dynamic binding, the actual method that does get called is deferred until run-time because it is being called with a dynamic parameter. There might be another overload ofGetNullableInt32that matches better the run-time value ofthisIsAnInt32. That alternate method, which cannot be known until run-time, might return some other type thanInt32?!As a result, the compiler, due to dynamic binding instead of static binding, cannot assume what the return type of the expression is at compile time and so the expression returns type dynamic. This can be seen by hovering over
var.You appear to have already come to a satisfactory explanation for your second question here: