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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:10:52+00:00 2026-06-16T15:10:52+00:00

I wrote this extension method : public static class A { public static IEnumerable<dynamic>

  • 0

I wrote this extension method :

public static class A
{
 public static IEnumerable<dynamic> AsDynamic<T>(this IEnumerable<T> f)
    {
        foreach (var element in f)
        {
                yield return (dynamic) element;
        }   
    }
}

And tested it :

List<int> l   = new List<int>(){1,2,3};
Console.WriteLine ( l.AsDynamic().GetType());

However the output is : typeof (IEnumerable<Object>)

  • Why it is not typeof (IEnumerable<dynamic>) ?

  • How can I make it to be like it ?

  • 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-16T15:10:53+00:00Added an answer on June 16, 2026 at 3:10 pm

    I think that you have a misunderstanding of what dynamic means. Essentially, when you tell the compiler that an object’s type is dynamic, you “promise” that the object at runtime will support whatever methods or properties that you invoke, in exchange for the compiler not complaining at compile time. You also promise that you will face the consequences if you break your promise.

    When you say that an object is dynamic, the compiler cannot make assumptions about the type, so it uses object, knowing that anything can be stored as object. When you make an IEnumerable<dynamic>, it becomes IEnumerable<object>, with one significant difference: you can call any method on its elements, and the compiler will not say a word:

    IEnumerable<SomeType> original = ...
    foreach (dynamic x in original.AsDynamic()) { // Using your method
        Console.WriteLine(x.SomeUnsupportedMethod()); // The compiler is silent!
    }
    

    Since original.AsDynamic() gives a sequence of dynamic objects, the compiler does not complain about your call to SomeUnsupportedMethod. If the method is indeed not supported at runtime, the program will crash; if the method is actually supported by elements of SomeType, there would be no crash, and the method will be invoked.

    That’s all the dynamic will do for you; statically, the “placeholder” will remain object, and typeof will tell you as much. But the exact capabilities of the object (its methods and properties) will not be examined until runtime.

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

Sidebar

Related Questions

I have a sorting extension method with the following signature: public static IEnumerable<T> CustomSort<T>(this
Lets say I have this extention method: public static bool HasFive<T>(this IEnumerable<T> subjects) {
I wrote this n-array tree class. I want to write a method to add
I have the following class and extension class (for this example): public class Person<T>
I wrote extension method to Timer class to destroy it after certain amount of
i have wrote a extension method for customize my validation messages, like this: namespace
I wrote the following extension method to get an element from a dictionary, or
I have the following method: public static TEventInvocatorParameters Until <TEventInvocatorParameters, TEventArgs>(this TEventInvocatorParameters p, Func<TEventArgs,
I'm trying to write a extension method of EventHandler , as followed: public static
I recently found out about C# extension methods and wrote this one: /// <summary>

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.