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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:05:23+00:00 2026-05-15T05:05:23+00:00

Not long time before I’ve discovered, that new dynamic keyword doesn’t work well with

  • 0

Not long time before I’ve discovered, that new dynamic keyword doesn’t work well with the C#’s foreach statement:

using System;

sealed class Foo {
    public struct FooEnumerator {
        int value;
        public bool MoveNext() { return true; }
        public int Current { get { return value++; } }
    }

    public FooEnumerator GetEnumerator() {
        return new FooEnumerator();
    }

    static void Main() {
        foreach (int x in new Foo()) {
            Console.WriteLine(x);
            if (x >= 100) break;
        }

        foreach (int x in (dynamic)new Foo()) { // :)
            Console.WriteLine(x);
            if (x >= 100) break;
        }
    }
}

I’ve expected that iterating over the dynamic variable should work completely as if the type of collection variable is known at compile time. I’ve discovered that the second loop actually is looked like this when is compiled:

foreach (object x in (IEnumerable) /* dynamic cast */ (object) new Foo()) {
    ...
}

and every access to the x variable results with the dynamic lookup/cast so C# ignores that I’ve specify the correct x‘s type in the foreach statement – that was a bit surprising for me… And also, C# compiler completely ignores that collection from dynamically typed variable may implements IEnumerable<T> interface!

The full foreach statement behavior is described in the C# 4.0 specification 8.8.4 The foreach statement article.

But… It’s perfectly possible to implement the same behavior at runtime! It’s possible to add an extra CSharpBinderFlags.ForEachCast flag, correct the emmited code to looks like:

foreach (int x in (IEnumerable<int>) /* dynamic cast with the CSharpBinderFlags.ForEachCast flag */ (object) new Foo()) {
    ...
}

And add some extra logic to CSharpConvertBinder:

  • Wrap IEnumerable collections and IEnumerator‘s to IEnumerable<T>/IEnumerator<T>.
  • Wrap collections doesn’t implementing Ienumerable<T>/IEnumerator<T> to implement this interfaces.

So today foreach statement iterates over dynamic completely different from iterating over statically known collection variable and completely ignores the type information, specified by user. All that results with the different iteration behavior (IEnumarble<T>-implementing collections is being iterated as only IEnumerable-implementing) and more than 150x slowdown when iterating over dynamic. Simple fix will results a much better performance:

foreach (int x in (IEnumerable<int>) dynamicVariable) {

But why I should write code like this?

It’s very nicely to see that sometimes C# 4.0 dynamic works completely the same if the type will be known at compile-time, but it’s very sadly to see that dynamic works completely different where IT CAN works the same as statically typed code.

So my question is: why foreach over dynamic works different from foreach over anything else?

  • 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-15T05:05:24+00:00Added an answer on May 15, 2026 at 5:05 am

    First off, to explain some background to readers who are confused by the question: the C# language actually does not require that the collection of a “foreach” implement IEnumerable. Rather, it requires either that it implement IEnumerable, or that it implement IEnumerable<T>, or simply that it have a GetEnumerator method (and that the GetEnumerator method returns something with a Current and MoveNext that matches the pattern expected, and so on.)

    That might seem like an odd feature for a statically typed language like C# to have. Why should we “match the pattern”? Why not require that collections implement IEnumerable?

    Think about the world before generics. If you wanted to make a collection of ints, you’d have to use IEnumerable. And therefore, every call to Current would box an int, and then of course the caller would immediately unbox it back to int. Which is slow and creates pressure on the GC. By going with a pattern-based approach you can make strongly typed collections in C# 1.0!

    Nowadays of course no one implements that pattern; if you want a strongly typed collection, you implement IEnumerable<T> and you’re done. Had a generic type system been available to C# 1.0, it is unlikely that the “match the pattern” feature would have been implemented in the first place.

    As you’ve noted, instead of looking for the pattern, the code generated for a dynamic collection in a foreach looks for a dynamic conversion to IEnumerable (and then does a conversion from the object returned by Current to the type of the loop variable of course.) So your question basically is “why does the code generated by use of the dynamic type as a collection type of foreach fail to look for the pattern at runtime?”

    Because it isn’t 1999 anymore, and even when it was back in the C# 1.0 days, collections that used the pattern also almost always implemented IEnumerable too. The probability that a real user is going to be writing production-quality C# 4.0 code which does a foreach over a collection that implements the pattern but not IEnumerable is extremely low. Now, if you’re in that situation, well, that’s unexpected, and I’m sorry that our design failed to anticipate your needs. If you feel that your scenario is in fact common, and that we’ve misjudged how rare it is, please post more details about your scenario and we’ll consider changing this for hypothetical future versions.

    Note that the conversion we generate to IEnumerable is a dynamic conversion, not simply a type test. That way, the dynamic object may participate; if it does not implement IEnumerable but wishes to proffer up a proxy object which does, it is free to do so.

    In short, the design of “dynamic foreach” is “dynamically ask the object for an IEnumerable sequence”, rather than “dynamically do every type-testing operation we would have done at compile time”. This does in theory subtly violate the design principle that dynamic analysis gives the same result as static analysis would have, but in practice it’s how we expect the vast majority of dynamically accessed collections to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.