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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:57:48+00:00 2026-06-06T07:57:48+00:00

I was looking for an anwer to question Get next N elements from enumerable

  • 0

I was looking for an anwer to question Get next N elements from enumerable didn’t find any satisfying and brewed my own. What I came up with was

IEnumerable<T> Chunk<T, R>(IEnumerable<R> src, int n, Func<IEnumerable<R>, T> action){
  IEnumerable<R> head;
  IEnumerable<R> tail = src;
  while (tail.Any())
  {
    head = tail.Take(n);
    tail = tail.Skip(n);
    yield return action(head);
  }
}

What I would really like though, is to have action have a default of t=>t, but I can’t figure out how to make that a default argument. The signature IEnumerable<T> Chunk<T, R>(IEnumerable<R> src, int n, Func<IEnumerable<R>, T> action = t=>t) gives a syntax error.

My question is, how do I do this?

I suppose this is identical to Specifying a lambda function as default argument but for C# instead of C++

As a side note, I know it doesn’t make any syntactical difference, but would it be easier to read if I switched T and R?

  • 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-06T07:57:49+00:00Added an answer on June 6, 2026 at 7:57 am

    Default values have to be constants, and the only constant value for a delegate is a null reference.

    I suggest you use overloading instead. Note that t => t wouldn’t be valid anyway, unless you know that there’s a conversion from IEnumerable<R> to T, which I don’t see anywhere.

    Aside from the lambda expression validity problem, you could use the null coalescing operator:

    IEnumerable<T> Chunk<T, R>(IEnumerable<R> src, int n,
                               Func<IEnumerable<R>, T> action = null)
    {
        action = action ?? (t => t);
        ...
    }
    

    … but that would be sort of abusing it, and you wouldn’t be able to tell whether the null was actually from a caller who thought they were passing a non-null value, and would prefer you to raise an ArgumentNullException.

    EDIT: Additionally, note that your method is fundamentally problematic – iterating over the chunks will evaluate the original sequence several times (once per chunk) in order to skip the right amount. It would probably be better to write a method which eagerly read each chunk before returning it. We have a similar method in MoreLINQ, called Batch. Note that Batch has exactly the overload mentioned here – and in this case, the t => t works, because the overload has fewer type parameters, so we know the identity conversion is okay.

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

Sidebar

Related Questions

I have been looking at the answer to this question: Pulling details from response
I need to find the next available ID (or key) from a fixed list
I posted this basic question before, but didn't get an answer I could work
This question seems to get asked a lot, but I can't find an answer
All day I was looking an answer on this question: How to log in
While looking up the answer to this question: Why is an out parameter not
I'm looking for an answer to a question similar to this one: protect users'
I was looking at an answer to an SO question today where the variable
I've been looking for an existing answer to this question, but haven't found one.
Hey guys i have a question which i have been looking for the answer

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.