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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:27:33+00:00 2026-05-18T02:27:33+00:00

Consider this method: public IEnumerable<T> GetList(int Count) { foreach (var X in Y) {

  • 0

Consider this method:

public IEnumerable<T> GetList(int Count)
{
    foreach (var X in Y)
    {
        // do a lot of expensive stuff here (e.g. traverse a large HTML document)
        // when we reach "Count" then exit out of the foreach loop
    }
}

and I’d call it like so: Class.GetList(10); which would return 10 items – this is fine.

I’d like to use IEnumerable‘s Take() method by using Class.GetList().Take(10) instead and I want the foreach loop to be able to somehow grab the number I passed into Take() – is this possible? It seems a more cleaner approach as it’s ultimately less expensive as I can grab a full list with Class.GetList() once and then use IEnumerable‘s methods to grab x items afterwards.

Thanks guys!

  • 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-18T02:27:34+00:00Added an answer on May 18, 2026 at 2:27 am

    You can do this by implementing your method as an iterator block:

    public IEnumerable<T> GetList()
    {
        foreach (var X in Y)
        {
            // do something
            yield return something;
        }
    }
    

    When you call Take(10) only the first 10 elements will be yielded – the rest of the method won’t be run.

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

Sidebar

Related Questions

Consider the following extension method in c#, Traverse: IEnumerable<T> Traverse<T>( this IEnumerable<T> source, Func<T,
Consider type like this one public interface IHaveGenericMethod { T1 Method<T1>(T1 parm); T2 Method<T1,T2>(T1
Consider this code (Java, specifically): public int doSomething() { doA(); try { doB(); }
Consider this trivial function: public static bool IsPositive(IComparable<int> value) { return value.CompareTo(0) > 0;
Consider this code: int size = 100 * 1000 * 1000; var emu =
Consider this class. public class DynamicField implements Comparable<DynamicField> { String title; int position; int
Consider this: public class TestClass { private String a; private String b; public TestClass()
consider this code block public void ManageInstalledComponentsUpdate() { IUpdateView view = new UpdaterForm(); BackgroundWorker
Consider this example table (assuming SQL Server 2005): create table product_bill_of_materials ( parent_product_id int
Consider this: var me = new { FirstName = John, LastName = Smith };

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.