Inline variable declaration doesn't compile when using '== false' instead of negation operator

后端 未结 1 1230
余生分开走
余生分开走 2020-12-31 11:11

Consider the following snippets:

void Foo(object sender, EventArgs e)
{
    if (!(sender is ComboBox comboBox)) return;
    comboBox.DropDownWidth = 100;
}
<         


        
相关标签:
1条回答
  • 2020-12-31 11:33

    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.

    0 讨论(0)
提交回复
热议问题