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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:31:00+00:00 2026-05-27T07:31:00+00:00

I made an extension method to find the number of consecutive values in a

  • 0

I made an extension method to find the number of consecutive values in a collection. Because it is generic, I allow the caller to define the “incrementor” which is a Func<> that is supposed to increment the value in order to check for the existence of a “next” value.

However, if the caller passes an improper incrementor (i.e. x => x), it will cause an infinite recursive loop. Any suggestions on a clean way to prevent this?

public static int CountConsecutive<T>(this IEnumerable<T> values, T startValue, Func<T, T> incrementor)
{
    if (values == null)
    {
        throw new ArgumentNullException("values");
    }
    if (incrementor == null)
    {
        throw new ArgumentNullException("incrementor");
    }
    var nextValue = incrementor(startValue);
    return values.Contains(nextValue)
        ? values.CountConsecutive(nextValue, incrementor) + 1
        : 1;
}
  • 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-27T07:31:00+00:00Added an answer on May 27, 2026 at 7:31 am

    In the purest sense, this is an attempt at the Halting Problem and is undecidable. For all but the simplest cases, you’ll have to trust those calling your method.

    Like others have shown, you can do a simple check of equality to show that the next value is different. Storing every visited T will work but you’ll have to worry about memory eventually.

    As an aside, here’s an easily implemented StackOverflowException so you have to be wary of any data set that will have a lot on consecutive values.

    var x = Enumerable.Range(1, 100000).CountConsecutive(1, x => x+1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've made a generic extension method that executes an action on an object and
Currently I'm using the following extension method that I made to retrieve the values
I made a chrome extension which calls jQuery's ajax method: content-script.js [...] $.ajax({ url:
I have an extension method called ToListIfNotNullOrEmpty() , which is hitting the DB twice,
I've made the following extension method ... public static class ObjectExtensions { public static
I have made my custom In extension method as shown below: public static class
I made an extension for IE9 that adds a toolbar button. The registration looks
About 2 weeks ago I nominated a Firefox Extension I made for public release.
I am using the (most popular) Bootstrap extension in Yii made by Chris83, and
I made a Web service in which I have a function to count some

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.