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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:16:06+00:00 2026-05-11T07:16:06+00:00

My problem is hard to explain, so I created an example to show here.

  • 0

My problem is hard to explain, so I created an example to show here.

When the WPF window in the example below is shown, three buttons are displayed, each one with a different text.

When anyone of these buttons is clicked, I assume its text should be displayed in the message, but instead, all of them display the same message, as if all of them were using the event handler of the last button.

public partial class Window1 : Window {     public Window1() {         InitializeComponent();         var stackPanel = new StackPanel();         this.Content = stackPanel;         var n = new KeyValuePair<string, Action>[] {              new KeyValuePair<string, Action>('I', () => MessageBox.Show('I')),              new KeyValuePair<string, Action>('II', () => MessageBox.Show('II')),              new KeyValuePair<string, Action>('III', () => MessageBox.Show('III'))         };         foreach (var a in n) {             Button b = new Button();             b.Content = a.Key;             b.Click += (x, y) => a.Value();             stackPanel.Children.Add(b);         }     } } 

Does anyone know what is wrong?

  • 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. 2026-05-11T07:16:07+00:00Added an answer on May 11, 2026 at 7:16 am

    It is because of how closures are evaluated compiler in the loop:

    foreach (var a in n) {     Button b = new Button();     b.Content = a.Key;     b.Click += (x, y) => a.Value();     stackPanel.Children.Add(b); } 

    The compiler assumes that you will need the context of a in the closure, since you are using a.Value, so it is creating one lambda expression that uses the value of a. However, a has scope across the entire loop, so it will simply have the last value assigned to it.

    To get around this, you need to copy a to a variable inside the loop and then use that:

    foreach (var a in n) {     Button b = new Button();     b.Content = a.Key;      // Assign a to another reference.     var a2 = a;      // Set click handler with new reference.     b.Click += (x, y) => a2.Value();     stackPanel.Children.Add(b); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a problem that is kinda hard to explain. So here's an example. Let's
It's little hard to explain problem, so better to show example. Test page Code
Its' hard to explain what i mean so here an example. class mysql {
here's what I need (it's a bit hard to explain): I have 2 window
This is a little bit of a hard problem to explain, so ill show
I have a problem that is a little hard to explain. I am building
This problem is going to be hard for me to explain but I will
I'm having a hard time trying to create a XmlRepository. The problem here I
It's hard to explain in a single line, my problem is that I need
Hard to explain the problem in the title, so please read on. I have

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.