Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6573649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:09:36+00:00 2026-05-25T15:09:36+00:00

I’ve run into a bit on an Anomaly where for the first time ever,

  • 0

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

  1. Why does shouldBeNullableInt32 get implicitly typed as a dynamic when the return type of GetNullableInt32 clearly returns a Nullable<Int32>?
  2. Why is the underlying Nullable<Int32> not preserved? Why a dynamic{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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:09:37+00:00Added an answer on May 25, 2026 at 3:09 pm
    1. Why does shouldBeNullableInt32 get implicitly typed as a dynamic when the return type of GetNullableInt32 clearly returns a Nullable<Int32>?

    This is because while it is apparent to us that GetNullableInt32 is 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 of GetNullableInt32 that matches better the run-time value of thisIsAnInt32. That alternate method, which cannot be known until run-time, might return some other type than Int32?!

    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:

    • C# 4: Dynamic and Nullable<>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.