Q:

C# - Dynamically Update Labels based on Dictionary Key

I have several labels I need to update on a C# form, and I would like to do so in a more efficient way. Currently, if a user clicks a checkbox, the checkbox ID is added to a dictionary, and a label is updated based on the value of the checkbox ID.
foreach (KeyValuePair<int, string> checkbox in CheckedDict)
{
  switch (checkbox.Key)
  {
    case 0:
      SomeLabel.Text = checkbox.Value;
      break;
    case 1:
      SomeOtherLabel.Text = checkbox.Value;
      break;
    ...
}

Any suggestions on how to make this better, faster, cleaner, etc?

A:

"Faster" is irrelevant here, because the C# compiler will turn that into a dictionary lookup if there are more than 6 or 7 different labels. Even if it didn't, the amount of time it takes to determine which label to update is going to be small compared to how long it takes to do the actual update.
You can make it cleaner, though, by creating a parallel dictionary that contains keys and labels. For example:
Dictionary<int, Label> labelDictionary = new Dictionary<int, Label>
{
    {0, SomeLabel},
    {1, SomeOtherLabel},
    ...
};

Then in your code:
foreach (KeyValuePair<int, string> checkbox in CheckedDict)
{
    labelDictionary[checkbox.Key].Text = checkbox.Value;
}

