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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:35:45+00:00 2026-05-13T16:35:45+00:00

Taking the following code, Resharper tells me that voicesSoFar and voicesNeededMaximum cause access to

  • 0

Taking the following code, Resharper tells me that voicesSoFar and voicesNeededMaximum cause “access to a modified closure”. I read about these but what puzzles me here is that Resharper suggests fixing this by extracting the variables to right before the LINQ query. But that is where they are already!

Resharper stops complaining if I merely add int voicesSoFar1 = voicesSoFar right after int voicesSoFar = 0. Is there some weird logic I do not understand that makes Resharper’s suggestion correct? Or is there a way to safely “access modified closures” in cases like these without causing bugs?

// this takes voters while we have less than 300 voices    
int voicesSoFar = 0;    
int voicesNeededMaximum = 300;    
var eligibleVoters =
    voters.TakeWhile((p => (voicesSoFar += p.Voices) < voicesNeededMaximum));
  • 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-13T16:35:45+00:00Added an answer on May 13, 2026 at 4:35 pm

    You have a very nasty problem that arises from mutating an outer variable to the lambda expression. The problem is this: if you try to iterate eligibleVoters twice (foreach(var voter in eligibleVoters) { Console.WriteLine(voter.Name); } and immediately after (foreach(var voter in eligibleVoters) { Console.WriteLine(voter.Name); }) you will not see the same output. That is just not right from a functional programming perspective.

    Here is an extension method that will accumulate until some condition on the accumulator is true:

    public static IEnumerable<T> TakeWhileAccumulator<T, TAccumulate>(
        this IEnumerable<T> elements,
        TAccumulate seed,
        Func<TAccumulate, T, TAccumulate> accumulator,
        Func<TAccumulate, bool> predicate
    ) {
        TAccumulate accumulate = seed;
        foreach(T element in elements) {
            if(!predicate(accumulate)) {
                yield break;
            }
            accumulate = accumulator(accumulate, element);
            yield return element;
        }
    }
    

    Usage:

    var eligibleVoters = voters.TakeWhileAccumulator(
                             0,
                             (votes, p) => votes + p.Voices, 
                             i => i < 300
                         );
    

    Thus the above says accumulate voices while we have accumulated less than 300 votes.

    Then with:

    foreach (var item in eligibleVoters) { Console.WriteLine(item.Name); }
    Console.WriteLine();
    foreach (var item in eligibleVoters) { Console.WriteLine(item.Name); }
    

    Output is:

    Alice
    Bob
    Catherine
    
    Alice
    Bob
    Catherine
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that is taking a while to process: self.itemsArray =
Taking over some code from my predecessor and I found a query that uses
I'm using the following code to rotate an image by an angle taking the
Taking following code: MyEntity e = dao.getEntity(1); e.setProp1(someVal); e.setProp2(otherVal); MyEntity eOld = dao.getEntity(1); If
The following code using ContentInfo(System.Security.Cryptography.Pkcs.ContentInfo) is really taking a long time to execute. Is
Taking the following code: // Bind the click event to the thumbnails. $(ul.hpList).on(click, a.hpThumb,
I have the following code running and it's taking me a long time to
I have a simple photo taking application that has the following design: [Consider the
I am taking help from the following link: http://www.codeproject.com/KB/cs/SMS.aspx I have Downloaded source and
I am taking the Current date by using the following sets of lines. java.util.Calendar

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.