- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我把代码放在下面,也上传到一个在线的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/
for /f "tokens=*" %%a in ('find /v ":" "%appdata%\gamelauncher\options.txt" ^| find "menu=a"') do (
我在 Javascript 中有一组全局计数器变量: var counter_0 = 0; var counter_1 = 0; var counter_2 = 0; 等等 然后我有一个 Javasc
好的,我正在阅读一些有关 RedBlackTrees 的代码。我注意到这一行“v1 = v2 = v3 = v4;”我理解类似“v1 += v2”(将 v2 添加到 v1 的当前值)和“v1 = v2
我正在为 C# 中的游戏数据加载制作一个 csv 阅读器,我想做的就是从数组(变量)的值声明一个变量,我们可以在 php 中像 $$foo 那样做。喜欢 void csvReader(string s
假设我有变量 内容为“ 123 ”和变量 b123 里面有一些文字。出于某种原因,我想使用变量 作为第二个 var 名称的一部分。像这样的东西: SET a=123 SET b123=some_tex
我对 javascript 有点陌生,我无法通过谷歌搜索找到任何内容,我正在编写一个程序,并且能够执行我所要求的操作: if (Variable == 1 或 Variable == 2 或 Vari
我发现我自己在做这种类型的 IF 语句分配。例如: if($variable == 1 || $variable == "whatever" || $variable == '492') { ...
我的虚拟 PC 在 MS-DOS 6.22 上运行时出现问题。 我需要使用变量 Date ,但我无法得到它,因为每当我尝试回显变量时,它都会显示 %variable%反而。 我在 Windows 控制
尝试运行此代码时: List list = em.createQuery("select balance b from Users where b.userName = '" + user_name.
我有一些代码,其中变量可以是 undefined、null 或正常值。无论变量是 undefined 还是 null,代码都需要做同样的事情。说有没有危险 for (var cur = this.bu
我正在编写一个批处理命令脚本,其中检查环境变量。我需要通过传递所有必需的变量来编写一个 FOR 循环,然后验证它是否已定义,如果未定义,则提示该键的值并永久设置该变量。 问题是我无法取消引用循环变量并
我知道这些是 Rails 的基础知识,但我仍然不知道 = 符号和 => 之间的全部区别以及 @some_variable 之间的区别、@@some_variable 和 :some_variable
我正在使用以下内容创建一个动态变量(PHP 术语中的“变量变量”): foo: "test1" set to-word (rejoin [foo "_result_data"]) array 5 但是
我一直在啃 PHP 套接字服务器和客户端的基础知识 here . 然后我偶然发现了这些行(摘自上面链接的第一个示例,发生在 while 中): if (false === ($buf = socket
这个问题在这里已经有了答案: What does "|=" mean? (pipe equal operator) (6 个答案) 关闭 9 年前。 我正在寻找一些编码来扩展我在 Java 方面的知
如何在 C++ 中从其他变量的值打印变量我只是 C++ 的新手。 在 php 中,我们可以通过其他变量的值来制作/打印一个变量。像这样。 $example = 'foo'; $foo = 'abc';
作为 Ruby on Rails 新手,我明白“@”和“:”引用有不同的含义。我看到了this post在 SO 中,其中描述了一些差异。 @ 表示实例变量(例如@my_selection) :表示别
编程新手/甚至更新。一个小的 go 程序有问题 - 不会编译带有 undefined variable 错误。代码: package main import ( "fmt" "io" "o
我知道其他一些语言,如PHP,支持“变量变量名”的概念--即,字符串的内容可以用作变量名的一部分。。我听说总的来说这不是一个好主意,但我认为它可以解决我在Python代码中遇到的一些问题。。有没有可能
我有两个版本的代码。 版本 1 Launcher.java class Launcher { public static void main(String[] args) {
我是一名优秀的程序员,十分优秀!