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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:58:37+00:00 2026-06-10T04:58:37+00:00

I just wrote some code that I don’t quite comprehend how it works. My

  • 0

I just wrote some code that I don’t quite comprehend how it works.

My question is about the local variables in the for loop which are then referenced when the radio button event occurs.

How does it track the different “versions” of these local variables and operate correctly? (i.e. the resulting radio buttons each fire an event with their corresponding value which is derived from an outer local variable)

public class RadioButtonPanel<T> : FlowLayoutPanel
{
    public RadioButtonPanel()
    {
        foreach (object value in Enum.GetValues(typeof(T)))
        {
            string name = Enum.GetName(typeof(T), value);
            var radioButton = new RadioButton { Text = name };
            radioButton.CheckedChanged += (s, e) =>
            {
                if (radioButton.Checked && this.Selected != null)
                    Selected((T)Enum.Parse(typeof(T), name));
            };
            this.Controls.Add(radioButton);
        }
    }

    public event SelectedEvent Selected;
    public delegate void SelectedEvent(T t);
}
  • 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-10T04:58:38+00:00Added an answer on June 10, 2026 at 4:58 am

    This is done via a Closure.

    Basically you can imagine that a small class has been created for you on your behalf, that has two properties for your local variables, and a single function. When your lambda gets called, it basically news one of those up and calls the function, thus preserving the values given to it.

    The C# specification actually has some really good examples of how this is done by the compiler. Specifically section 6.5.3

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

Sidebar

Related Questions

I just wrote some code. It has my own custom made class. In that
I've just wrote some code (and it works) for displaying some text near the
I just wrote some simple sample code to make sure that I had EclEmma
How do you guys write comments on some simple code that you use just
I just wrote some code to test the behavior of std::equal, and came away
I am cleaning up some code in a C# app that I wrote and
I wrote some code to tackle a work-related problem. The idea is that the
I wrote some code in Java using the pdfbox API that splits a pdf
I just wrote some C code: #include <stdlib.h> #include <time.h> #include <string.h> typedef struct
I have some code that I wrote to implement a vertical swipe on a

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.