Q:

Is there a better/cleaner way to assign values based on the result of radio buttons?

I have 3 radio buttons all grouped inside a group box in a WinForms project. Currently, I am checking which button is checked and assigning values using the following if/else statement, similar to the below example
        if (rb1.Checked)
        {
            selectedButton = 1;
            selectedButtonText = rb1.Text;
        }
        else if (rb2.Checked)
        {
            selectedButton = 2;
            selectedButtonText = rb2.Text;
        }
        else
        {
            selectedButton = 3;
            selectedButtonText = rb3.Text;
        }

        Console.WriteLine(selectedButtonText);

This doesn't look very efficient or scalable. Is there a better way to do this? 

A:

you can use LINQ with Controls to get the checked one. 
var radio = groupBox.Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked);

switch(radio.Name)
{
    case "rb1":
        selectedButton = 1;
        selectedButtonText = rb1.Text;
        break;
    case "rb2":
        selectedButton = 2;
        selectedButtonText = rb2.Text;
        break;
    default:
        selectedButton = 3;
        selectedButtonText = rb3.Text;
        break;
}

Console.WriteLine(selectedButtonText);

However, I think you're best if you use something like ComboBox instead of the radio buttons. 

