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

  • Home
  • SEARCH
  • 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 85261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:03:41+00:00 2026-05-10T22:03:41+00:00

How do I define an Extension Method for IEnumerable<T> which returns IEnumerable<T> ? The

  • 0

How do I define an Extension Method for IEnumerable<T> which returns IEnumerable<T>? The goal is to make the Extension Method available for all IEnumerable and IEnumerable<T> where T can be an anonymous type.

  • 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. 2026-05-10T22:03:42+00:00Added an answer on May 10, 2026 at 10:03 pm

    The easiest way to write any iterator is with an iterator block, for example:

    static IEnumerable<T> Where<T>(this IEnumerable<T> data, Func<T, bool> predicate) {     foreach(T value in data)     {         if(predicate(value)) yield return value;     } } 

    The key here is the ‘yield return‘, which turns the method into an iterator block, with the compiler generating an enumerator (IEnumerator<T>) that does the same. When called, generic type inference handles the T automatically, so you just need:

    int[] data = {1,2,3,4,5}; var odd = data.Where(i=>i%2 != 0); 

    The above can be used with anonymous types just fine.

    You can, of coure, specify the T if you want (as long as it isn’t anonymous):

    var odd = data.Where<int>(i=>i%2 != 0); 

    Re IEnumerable (non-generic), well, the simplest approach is for the caller to use .Cast<T>(...) or .OfType<T>(...) to get an IEnumerable<T> first. You can pass in this IEnumerable in the above, but the caller will have to specify T themselves, rather than having the compiler infer it. You can’t use this with T being an anonymous type, so the moral here is: don’t use the non-generic form of IEnumerable with anonymous types.

    There are some slightly more complex scenarios where the method signature is such that the compiler can’t identify the T (and of course you can’t specify it for anonymous types). In those cases, it is usually possible to re-factor into a different signature that the compiler can use with inference (perhaps via a pass-thru method), but you’d need to post actual code to provide an answer here.


    (updated)

    Following discussion, here’s a way to leverage Cast<T> with anonymous types. The key is to provide an argument that can be used for the type inference (even if the argument is never used). For example:

    static void Main() {     IEnumerable data = new[] { new { Foo = 'abc' }, new { Foo = 'def' }, new { Foo = 'ghi' } };     var typed = data.Cast(() => new { Foo = 'never used' });     foreach (var item in typed)     {         Console.WriteLine(item.Foo);     } }  // note that the template is not used, and we never need to pass one in... public static IEnumerable<T> Cast<T>(this IEnumerable source, Func<T> template) {     return Enumerable.Cast<T>(source); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 175k
  • Answers 175k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer running any service as root has risk. It's hard to… May 12, 2026 at 3:01 pm
  • Editorial Team
    Editorial Team added an answer You need to specify a context (the form) for the… May 12, 2026 at 3:01 pm
  • Editorial Team
    Editorial Team added an answer This will read exactly one line and ensure that the… May 12, 2026 at 3:01 pm

Related Questions

I am trying to develop some general purpose custom ValidationAttributes. The fact that one
How can I detect in my python script if its being run by the
For embedded applications, it is often necessary to access fixed memory locations for peripheral
Basically I want to implement a simple Rails extension to define the seriousness of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.