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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:06:25+00:00 2026-05-24T03:06:25+00:00

I’ve got code like the following: private void SetupCheeseShop(Button buyCheese, Button spoilCheese) { var

  • 0

I’ve got code like the following:

private void SetupCheeseShop(Button buyCheese, Button spoilCheese)
{
    var cheeseCount = 0;    // No cheese

    spoilCheese.Click += (sender, e) => {
        // "Access to Modified Closure" warning occurs for cheeseCount below:
        MessageBox.Show(string.Format("{0} cheeses have spoiled", cheeseCount));

        cheeseCount = 0;    // Throw out moldy cheese
    };


    buyCheese.Click += (sender, e) => {
        cheeseCount++;
    };
}

ReSharper is warning me that I’m accessing a modified closure when reading from cheeseCount in the spoil-cheese handler. Can I safely ignore it in this case?

I’m expecting the cheese count to be modified between calls to the first closure, but I’m not sure what happens when the code doing the modifying is in a second closure around the same variable.

  • 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-24T03:06:27+00:00Added an answer on May 24, 2026 at 3:06 am

    You can safely ignore the warning in your case. It warns you that the closed over variable can potentially have a different value than the current value (0) at the time the closure is created. That’s what you want here. You can have any number of closures, they all reference the same variable. Think of a closure variable as a field in a class (which is what the compiler generates).

    As stated in section §5.1.7 of the C# Language Specification 4.0:

    If the local variable is captured by an anonymous function
    (§7.15.5.1), its lifetime extends at least until the delegate or
    expression tree created from the anonymous function, along with any
    other objects that come to reference the captured variable, are
    eligible for garbage collection.

    You can safely rely on this behavior: the variable stays the same, no matter how many anonymous functions are referencing it, until the event handlers are collected, which won’t happen until the buttons themselves are collected in your example.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string

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.