我把代码放在下面,也上传到一个在线的c#编译器: jdoodle.com/a/1jww 代码可以在线编译和运行,但是,它不能在我本地的 visual studio 中编译。
我正在使用:
Visual Studio 2017 15.9.13,
Console app, .Net Framework 4.7.2
Language version c# 7.3
Microsoft (R) Visual C# Compiler version 2.10.0.0 (b9fb1610)
代码如下:
class Program
{
static void Main()
{
Dictionary<string,int> myDict = new Dictionary<string,int>();
myDict.Add("hello", 1);
if (myDict?.TryGetValue("hello", out var value) == true)
{
Console.WriteLine("Hello" + value.ToString());
}
}
}
期望在 Console.Output 中看到 Hello1 因为如果条件为真,则 Null-Conditional 检查必须返回一个非空值,并且该键存在于字典中并且从 TryGetValue 方法返回时必须已经分配了该值。
因为根据the documentation :
the called method is required to assign a value before the method returns.
更新:
这是 https://github.com/dotnet/roslyn/issues/32572 中的未决问题
如果对编译器的要求包括不发出误报,我认为这是一个真正的问题/错误。
我的观点:
每当 CPU 执行到 if 括号代码块内的点时,该值必须从 TryGetValue 调用返回并且不是“未分配的局部变量”。如果编译器在解释 null 条件运算符时无法期待赋值状态,则更简单的解决方案是给出诸如“无法确定赋值状态”之类的警告而不是错误。
最佳答案
这是由于编译器的不同。
在这个 fiddle 中,https://dotnetfiddle.net/5GgGNS ,你可以看到错误,在单声道编译器中被忽略了。
我认为错误是有效的,因为这一行
if (myDict?.TryGetValue("hello", out var value) == true)
不保证初始化局部变量value
。
如果您将其重写为:
if (myDict?.TryGetValue("hello", out var value) == null)
它会尝试访问值
。
现在,null
值(在您的情况下为 true
)可能是函数的返回值,只有在运行时才知道。
但是,由于所有变量基本上都是 always initialized ,它只是一个编译器功能。
另一方面,根据 C#5 规范:
A local variable introduced by a local-variable-declaration is not automatically initialized and thus has no default value. For the purpose of definite assignment checking, a local variable introduced by a local-variable-declaration is considered initially unassigned. A local-variable-declaration may include a local-variable-initializer, in which case the variable is considered definitely assigned only after the initializing expression (§5.3.3.4).
但是您的代码是 C# 6。
所以我的结论是编译器对它的解释不同。 Microsoft 编译器将 ?.
运算符考虑在内。您应该将其作为错误归档,或者至少在双方中找到。
论证
有趣的是,如果你使用这段代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
//Your code goes here
Dictionary<string,int> myDict = null;
if (myDict?.TryGetValue("hello", out var value) == null)
{
Console.WriteLine("Hello" + value.ToString());
}
}
}
[使用https://www.jdoodle.com/compile-c-sharp-online , 单声道 5.10.1]
您将在工作中看到对 default(T)
的实际初始化。输出是 Hello0
。尽管如此,它还是值得注意的,因为由于 ?
,以及 myDict
为 null
的事实,TryGetValue
不应该被调用并留下 value
“未初始化”。
The null-conditional operators are short-circuiting. That is, if one operation in a chain of conditional member or element access operations returns null, the rest of the chain doesn't execute.
但是...,因为没有未初始化的变量;如果它编译,编译器将确保它的行为不是未定义的。
因此,由于 value
已在运行时初始化,因此问题仍然存在,它是否是构建时的有效编译器错误。关于代码的运行时意图(这就是错误首先出现的原因),但我认为它仍然是一个灰色区域。
请注意,根据 this default(T)
不可覆盖,这实际上不会导致失败。
通过运行这个小测试:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
//Your code goes here
Dictionary<string,int> myDict = null;
if (myDict?.Bar(out var test) == null)
{
Console.WriteLine("does hit");
}
}
}
static class Foo
{
public static object Bar(this Dictionary<string,int> input, out int test)
{
test = 3;
Console.WriteLine("does not hit");
return 1;
}
}
[使用https://www.jdoodle.com/compile-c-sharp-online , 单声道 5.10.1]
输出变为:
does hit
并且您可以验证 ?.
运算符的正确运行时行为。
关于c# - 为什么编译器抛出错误 CS0165 : Use of unassigned local variable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56779825/