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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:40:12+00:00 2026-05-15T15:40:12+00:00

I want to be able to ask, at run time, an IEnumerable if it

  • 0

I want to be able to ask, at run time, an IEnumerable if it is a deferred expression or if it is a concrete collection.

So, if the method was called IsDeferred, then IsDeferred( myList.Where( i => i > 5 ) ) would return true and IsDeferred( myList.Where( i => i > 5 ).ToList() ) would return false.

Thanks.

EDIT:

I thought I would be able to ask this without providing the underlying reason I want to do it, but I guess not. First, as others have pointed out, there is no way to tell this at compile time. The type of a collection cannot necessarily tell you whether it is lazy or not. You can have one IEnumerable which is a deferred query and another which is not (see original question). Using brute force to identify concrete types is not an elegant solution.

Now, as for the reason I want to do this. Imagine a method that takes in an IEnumerable and then references it several times:

public void MyMethod<T>( IEnumerable<T> items) {
  foreach( var item in items )
    // Do stuff.
  Console.WriteLine( "There are " + items.Count() + " items in the collection." );
  if( items.Any() )
    // Do some more things.
}

Now, this looks fine, but if I call MyMethod( myList.Where( i => i.ReallyExpensiveOperation() ) ), then you can see that the expensive Where is going to get executed three times. Once for the iteration, once for the Count, and once more for the Any. I could solve this by making the first line of the MyMethod do a ToList(). But, it would be better if I could not do that if I knew I didn’t have to (like, if I knew it was a concrete list already). I understand I could re-write (the completely fake and not at all a real-world example) MyMethod to not reference the items collection multiple times, but I am not interested in that as a solution.

Thanks again.

  • 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-15T15:40:12+00:00Added an answer on May 15, 2026 at 3:40 pm

    it is impossible to create this method for all situations, because only the creator of class knows is it lazy or not. Example: class can be created that prereads values into inner list or not based on some config – you will never to be able to guess. the best you can is to create list of well known types that are certain concrete collections (List, Array, etc.) and check against it. I think that you are working in wrong direction – because “lazyness” of collection should be known at compile time or be configurable (not guessable).

    Edit: You can force collection to be eager by calling .ToList (or .ToArray). Call to those methods will force iteration immediately.

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

Sidebar

Related Questions

Want to be able to provide a search interface for a collection of objects
I have an aggregated attribute which I want to be able ask about its
I'm using LaTeX and BibTeX for an article, and I want to able to
I want to be able to email a report daily from a glpi database
I want to be able to open my website and use some kind of
I want to be able to use mklink to create symbolic links on my
I want to be able to invoke an SSIS package at will from a
I want to be able to do some profiling to some app using the
I want to be able to trim images, many of which are very long
I want to be able to click a button and open a new form

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.