使用“== false”而不是否定运算符时,内联变量声明不会编译

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

考虑以下片段:

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

相比
void Bar(object sender, EventArgs e)
{
    if ((sender is ComboBox comboBox) == false) return;
    comboBox.DropDownWidth = 100;
}

包含 Foo() 的代码在 .Net 4.6.1 中成功编译,而包含 Bar() 的代码生成 Use of unassigned local variable 'comboBox'

不讨论使用 == false 而不是否定运算符背后的原因,有人可以解释为什么一个编译而另一个不编译吗?

更新答案感谢Julien打开GitHub问题。

查看 Neal Gafter 的回复(从 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.


我相信 comboBox 变量只有在模式匹配时才会被创建