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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:19:59+00:00 2026-06-12T00:19:59+00:00

int count = itemsToValidate.Count; foreach(var item in itemsToValidate) { item.ValidateAsync += (x, y) =>

  • 0
int count = itemsToValidate.Count;
foreach(var item in itemsToValidate)
{
   item.ValidateAsync += (x, y) => this.HandleValidate(ref count);
}

private void HandleValidate(ref int x)
{
  --x;
  if (x == 0)
  {
       // All items are validated.
  }
}

For the above code resharper complained “Access to Modified Closure”. Doesn’t do that if I change that to type of object. Why is this a closure, even though I am passing by ref ?

  • 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-12T00:20:01+00:00Added an answer on June 12, 2026 at 12:20 am

    This happens all the time

    ReSharper is warning you that count is implicitly captured by the lambdas that you are assigning as “validation complete” event handlers, and that its value may well change between the time the lambda is created (i.e. when you assign the event handler) and the time when it is invoked. If this happens, the lambda will not see the value one would intuitively expect.

    An example:

    int count = itemsToValidate.Count;
    foreach(var item in itemsToValidate)
    {
       item.ValidateAsync += (x, y) => this.HandleValidate(ref count);
    }
    
    // afterwards, at some point before the handlers get invoked:
    count = 0;
    

    In this instance the handlers will read the value of count as 0 instead of itemsToValidate.Count — which might be called “obvious”, but is surprising and counter-intuitive to many developers not familiar with the mechanics of lambdas.

    And we usually solve it like this

    The usual solution to “shut R# up” is to move the captured variable in an inner scope, where it is much less accessible and R# can be prove that it cannot be modified until the lambda is evaluated:

    int count = itemsToValidate.Count;
    foreach(var item in itemsToValidate)
    {
       int inner = count; // this makes inner impossible to modify
       item.ValidateAsync += (x, y) => this.HandleValidate(ref inner);
    }
    
    // now this will of course not affect what the lambdas do
    count = 0;
    

    But your case is special

    Your particular case is a comparatively rare one where you specifically want this behavior, and using the above trick would actually make the program behave incorrectly (you need the captured references to point to the same count).

    The correct solution: disable this warning using the special line comments that R# recognizes.

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

Sidebar

Related Questions

Consider this method: public IEnumerable<T> GetList(int Count) { foreach (var X in Y) {
@{int count = 0;} @foreach (var item in Model.Resources) { @(count <= 3 ?
If I have a method like this: public void DoSomething(int Count, string[] Lines) {
Consider this C code: #include stdio.h int main(void) { int count = 5; unsigned
This is my code - (void)updateCounterImage:(NSTimer *)theTimer { static int count = 0; count
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { int count = jSlider1.getValue(); int delay = jSlider2.getValue(); int
I have some code, int count = 0; list.ForEach(i => i.SomeFunction(count++)); This seems to
I have this struct; #define BUFSIZE 10 struct shared_data { pthread_mutex_t th_mutex_queue; int count;
Say I have a function: void someFunc(int *x,int count); which is out of my
I have this code: public static String SelectRandomFromTemplate(String template,int count) { String[] split =

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.