I have the following code:
foreach (var control in this.Controls)
{
}
I want to do something like control.Hide() in there. But the items in the this.Controls collection are not of type Control (they are Object).
I can’t seem to remember the safe way to cast this to call hide if it is really of type Control and do nothing otherwise. (I am a transplanted delphi programmer and I keep thinking something like control is Control.)
Here’s a case where you don’t want to use var.
does exactly what you want.
Test it if you do not believe.
For other scenarios where you could potentially have a mixed collection, you could do something like