Clearing all controls I'm clearing all the controls on a windows form with the following

form.Controls.Cast&lt;dynamic&gt;().ToList().ForEach(c =&gt;
{
      switch (c)
      {
          case CheckBox t when c is CheckBox:
              c.Checked = false;
              break;
          case System.Windows.Forms.ComboBox t when c is System.Windows.Forms.ComboBox:
              c.Items.Clear();
              break;
          case CheckEdit t when c is CheckEdit:
              c.Checked = false;
              break;
          default:
              c.Text = "";
              break;
      }
});


I have to use dynamic because if I dont, I wont get the options for Checked and I also wont be able to clear any items in a combobox or list for that matter. Is there a neater way of achieving this? I'm using c# 7.3 which allows me to use the when keyword, I wasn't able to find how to do this with anything under 7.0, If there is something for under 7.0, what would it be?
I think you're rather over-complicated this for yourself! A much better solution is to use the pattern matched results (which you are already creating (t)) (this requires C# 7.0 still, I believe).



foreach (Control c in form.Controls)
{
    switch (c)
    {
        case CheckBox cb:
            cb.Checked = false;
            break;
        case ComboBox cb:
            cb.Items.Clear();
            break;
        case CheckEdit ce:
            ce.Checked = false;
            break;
        default:
            c.Text = "";
            break;
    }
}



No dynamic here: Text is already a member of Control.
Foreach loops are easier to read than .Foreach (in my opinion, at least; even if they are a bit funny underneath)
Your original ToList was redundant: no need to cache a result you are going to stream immediately in single-threaded logic
I've renamed t to something slightly less terrible; since they are very local variables indeed, I personally wouldn't worry to much about the names.
Note that this code will also clear the text on labels, and buttons, and everything else... so it seems an odd thing to provide as a form-wide operation.