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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:57:35+00:00 2026-06-11T12:57:35+00:00

From this answer https://stackoverflow.com/a/6457528/299110 I’m using ctrl.PreRender += (sender, e) => ControlPreRender(ctrl, rule); to

  • 0

From this answer https://stackoverflow.com/a/6457528/299110

I’m using ctrl.PreRender += (sender, e) => ControlPreRender(ctrl, rule); to one or more controls within a foreach, with the values of ctrl and rule changing each time.

However when the ControlPreRender method is called, the rule parameter seems out of line with the sender the event handler was attached to.

I know I’m missing something here, not sure what though!

Update:
Thanks for the answers, Eric Lippert’s blogs really explained it. As suggested by the down-voter, I’ve put more of the code below, hopefully improving the question a bit:

foreach (var ctrl in controls) 
{
    // ...
    foreach (var rule in rules)
    {
        // ...
        ctrl.PreRender += (sender, e) => ControlPreRender(ctrl, rule);
    }
}

public static void ControlPreRender(Control ctrl, ControlRule rule)
{
    // ...
}
  • 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-11T12:57:36+00:00Added an answer on June 11, 2026 at 12:57 pm

    I think you want a temporary variable:

    foreach(var rule in rules)
    {
        var tmpRule = rule;
        ctrl.PreRender += (sender, e) => ControlPreRender(sender as Control, tmpRule);
    }
    

    The reason is the following: Without that temporary variable, all your anonymous methods reference the same instance which changes as you loop through all your rules. This is called “access to modified closure”. As erikkallen mentions, this has been fixed in C# 5.

    You can easily check that yourself: Set a breakpoint in ControlPreRender and on the first breakpoint hit make an object ID for the rule parameter. You will see that at all the following hits of your breakpoint the rule parameter will have the same object ID which means it is the exact same instance.

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

Sidebar

Related Questions

I am using the script from this answer https://stackoverflow.com/a/1681410/22 to insert a launch application
Using the code from this answer: https://stackoverflow.com/a/9744961/514773 I've noticed that any time I enter:
I found this question https://stackoverflow.com/questions/4612563/get-push-notification-from-twitter-on-status-updates , but there was no answer. I'm looking to
Here is a quote from https://stackoverflow.com/users/893/greg-hewgill answer to Explain Python's slice notation . Python
This is a slightly modified example from the Java Sound info page. https://stackoverflow.com/tags/javasound/info Unfortunately,
I am using the accepted answer from this question . The relevant code is
I am trying to integrated linkedIn using this question answer Posting LinkedIn message from
ANSWER https://stackoverflow.com/a/12507520/962890 it was so trivial.. args! but lots of good information received. thanks
Let me reformulate, as the answer https://stackoverflow.com/questions/951907/where-are-my-visitors-going was absolutely correct, but my question not
From a comment on https://stackoverflow.com/a/11064/247702 You save the query planner from needing to figure

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.