编译器错误地指示使用未分配的局部变量错误

Dan*_*rak 6 c# nullable compiler-errors out null-propagation-operator

鉴于此代码:

private void TryIt(Dictionary<int, int> myDict)
{
    if (myDict?.TryGetValue(1, out int myValue) ?? false)
    {
        Console.Out.WriteLine(myValue); // <-- Error CS0165
    }
}
Run Code Online (Sandbox Code Playgroud)

C# 编译器发出:

error CS0165: Use of unassigned local variable 'myValue'
Run Code Online (Sandbox Code Playgroud)

但当操作员跳过调用myValue时,显然无法引用。这是因为结果被转换为by 。TryGetValue()?.nullfalse?? false

换句话说,如果myDictis null?.操作员将跳过对 的调用TryGetValue(),保持myValue未分配状态。我明白了。

但是??操作符将始终评估到 的 null 传播false,从而防止在这种情况下进入 if 块。

这在编译时很明显,那么为什么会出现错误呢?

我怀疑这可能与所有这些语法糖最终如何分解为实际的 .NET p 代码有关,但错误似乎仍然是错误的......

不使用.?运算符时,我不会收到任何错误,这是预期的:

    if (myDict.TryGetValue(1, out int myValue))
    {
        Console.Out.WriteLine(myValue); // <-- NO ERROR
    }
Run Code Online (Sandbox Code Playgroud)

只是当我使用.?with时?? false

Hen*_*man 2

“但这显然不可能……”

这是正确的,但编译器不会深入跟踪逻辑。

编译器可以推断出这一点,但请注意 myValue 的范围超出了 if 语句:

if (myDict?.TryGetValue(1, out int myValue) ?? false)
{
    Console.Out.WriteLine(myValue); // <-- Error CS0165
}
Console.Out.WriteLine(myValue); // myValue is in scope here
Run Code Online (Sandbox Code Playgroud)

因此,虽然您可能希望编译器弄清楚所有的?.??逻辑,并且 中的代码if () { ... }是一个特殊情况,但这显然是一个被认为不够重要的功能。

myDict?.TryGetValue(1, out int myValue)并不总是分配给myValue.

  • “as `.TryGetValue()` returns bool” - 是的,但是 `?.TryGetValue()` 可能返回 bool 或 null,因此它变成 `bool?` (2认同)