Consider the following piece of code:
This may appear to be correct but, in actual fact, only the last value of str variable will be used whenever any button is clicked. The reason for this is that foreach unrolls into a while loop, but the iteration variable is defined outside this loop. This means that by the time you show the message box, the value of str may have already been iterated to the last value in the strings collection.
A solution to this requires creating an internal reference, e.g.:
The code above ensures that the correct reference is passed into the message box.