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 9192407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:54:57+00:00 2026-06-17T20:54:57+00:00

I (lazily) used var in the original version of the below code and got

  • 0

I (lazily) used var in the original version of the below code and got a strange runtime exception in an entirely different part of the code. Changing “var” to “int” fixed the runtime exception but I cannot quite see why. I boiled the code down to this example;

public class Program
{
    private static List<string> Test(string i) { return new List<string> {i}; }
    private static dynamic GetD() { return 1; }

    public static void Main()
    {
        int value1 = GetD();   // <-- int
        var result1 = Test("Value " + value1);
        // No problem, prints "Value 1", First() on List<string> works ok.
        Console.WriteLine(result1.First());

        var value2 = GetD();   // <-- var
        var result2 = Test("Value " + value2);
        // The below line gives RuntimeBinderException 
        // 'System.Collections.Generic.List<string>' does not contain a 
        // definition for 'First'
        Console.WriteLine(result2.First());
    }
}

I can see the type of the “var” being dynamic instead of int, but why does that type propagate to and affect the behaviour of the return value of the call to Test()?

EDIT: Maybe I should clarify my question; I can see that dynamic propagates to result2, what I cannot understand is why, when the IDE clearly indicates that List<string> Test(string) is the method called, it still infers the return value as dynamic. Is it a case of the IDE being more clever than the compiler?

  • 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-06-17T20:54:58+00:00Added an answer on June 17, 2026 at 8:54 pm

    Your code is compiled like this:

    public static void Main()
    {
        int value1 = GetD();   // <-- int
        List<string> result1 = Test("Value " + value1);
        // No problem, prints "Value 1", First() on List<string> works ok.
        Console.WriteLine(result1.First());
    
        dynamic value2 = GetD();   // <-- var
        dynamic result2 = Test("Value " + value2);
        // The below line gives RuntimeBinderException 
        // 'System.Collections.Generic.List<string>' does not contain a 
        // definition for 'First'
        Console.WriteLine(result2.First());
    }
    

    result2 is a dynamic object, then extention method is not supported on it (used as extention method).

    However, you can do this:

    Console.WriteLine(Enumerable.First(result2));
    

    UPDATE

    Your IDE is not so clever. Try to add the new method:

    private static List<int> Test(int i) { return new List<int> { i }; }
    

    It will propose you the two possibilities.

    UPDATE2

    Paragraph 7.6.5 of the C# specification:

    An invocation-expression is dynamically bound (§7.2.2) if at least one
    of the following holds:

    • The primary-expression has compile-time type dynamic.
    • At least one argument of the optional argument-list has compile-time type dynamic and the primary-expression does not have a delegate type.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C#, the following code (from this page) can be used to lazily instantiate
Despite using configuration below I got lazy initialize error: org.hibernate.LazyInitializationException: failed to lazily initialize
I've got an entity with a few properties that gets used a lot in
Here is some code I've been twiddling with to try and lazily fill in
I think that I'm probably not writing lazily instantiated methods/attributes in the most ruby
Is it possible to have a list be evaluated lazily in Python? For example
I found an interesting bug in a program that I implemented somewhat lazily, and
In Hibernate Envers, all related collections of an entity are loaded lazily, regardless of
I have a UIViewController that should lazily load a view and then keep it
What is the proper way to implement a getter method for a lazily-initialized member

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.