Consider the following snippets:
void Foo(object sender, EventArgs e)
{
if (!(sender is ComboBox comboBox)) return;
comboBox.DropDownWidth = 100;
}
<
Updated Answer Thanks to Julien opening a GitHub issue.
See Neal Gafter's response (copied here from here):
However, the error you're seeing is not about scope. It is about definite assignment. A pattern variable is definitely assigned when the pattern-matching expression is true. The unary ! operator reverses assigned-when-true and assigned-when-false. However, the boolean equality operator == throws away the distinction between assigned-when-true and assigned-when-false.
I believe the comboBox
variable will only be created if the pattern matches.