"Use of unassigned local variable" 在带有 TryParse 的 if 语句中使用动态
"Use of unassigned local variable" in if statement with TryParse with the use of dynamic
我刚刚在 VS2015 .Net v4.5.2 控制台应用程序中输入了以下代码:
dynamic fromString = "blah", toString = "blah2";
DateTime fromDate, toDate;
if (DateTime.TryParse(fromString.ToString(), out fromDate) && DateTime.TryParse(toString.ToString(), out toDate)) {
Console.WriteLine(fromDate);
Console.WriteLine(toDate);
}
有点意外,我收到错误 "Use of unassigned local variable toDate"。我没有预料到它,因为仅当 'toDate' 从第二个 TryParse.
中赋值时,才会输入 if 语句
不用说,它可以通过分配 'toDate' 一个值来解决:
DateTime fromDate, toDate = DateTime.MinValue;
或将 && 更改为 &,以便无论第一次失败都执行两个 TryParses。
但是,我想知道为什么会出现错误?如果变量 fromString 和 toString 是字符串,则不会发生错误,编译器也不会给出 toDate 未分配的错误。因此我想知道为什么编译器会以不同的方式对待 string
和 dynamic.ToString()
?
这是因为您使用了短路运算符 &&,这意味着如果第一个 TryParse returns 为假,则永远不会执行第二个 TryParse,从而使 ToDate 变量未分配。
尝试一下,将 && 替换为 &,您的错误就会消失,因为现在将始终执行两个 TryParse 调用。
编译器不够聪明(它不分析你的逻辑)知道在某些情况下不会执行里面的代码。
编辑:@Simon,我重新阅读了你的问题,发现你已经知道了这一点......也许是因为 .ToString 总是存在于一个对象上但并不总是存在于一个动态对象上(例如当它是一个com 对象),在这种情况下,编译器会减少检查?
这是 Roslyn 中的重大更改,记录在案 here:
The definite assignment rules implemented by previous compilers for dynamic expressions allowed some cases of code that could result in variables being read that are not definitely assigned. See https://github.com/dotnet/roslyn/issues/4509 for one report of this.
[截取示例]
Because of this possibility the compiler must not allow this program to be compiled if val has no initial value. Previous versions of the compiler (prior to VS2015) allowed this program to compile even if val has no initial value. Roslyn now diagnoses this attempt to read a possibly uninitialized variable.
我刚刚在 VS2015 .Net v4.5.2 控制台应用程序中输入了以下代码:
dynamic fromString = "blah", toString = "blah2";
DateTime fromDate, toDate;
if (DateTime.TryParse(fromString.ToString(), out fromDate) && DateTime.TryParse(toString.ToString(), out toDate)) {
Console.WriteLine(fromDate);
Console.WriteLine(toDate);
}
有点意外,我收到错误 "Use of unassigned local variable toDate"。我没有预料到它,因为仅当 'toDate' 从第二个 TryParse.
中赋值时,才会输入 if 语句不用说,它可以通过分配 'toDate' 一个值来解决:
DateTime fromDate, toDate = DateTime.MinValue;
或将 && 更改为 &,以便无论第一次失败都执行两个 TryParses。
但是,我想知道为什么会出现错误?如果变量 fromString 和 toString 是字符串,则不会发生错误,编译器也不会给出 toDate 未分配的错误。因此我想知道为什么编译器会以不同的方式对待 string
和 dynamic.ToString()
?
这是因为您使用了短路运算符 &&,这意味着如果第一个 TryParse returns 为假,则永远不会执行第二个 TryParse,从而使 ToDate 变量未分配。
尝试一下,将 && 替换为 &,您的错误就会消失,因为现在将始终执行两个 TryParse 调用。
编译器不够聪明(它不分析你的逻辑)知道在某些情况下不会执行里面的代码。
编辑:@Simon,我重新阅读了你的问题,发现你已经知道了这一点......也许是因为 .ToString 总是存在于一个对象上但并不总是存在于一个动态对象上(例如当它是一个com 对象),在这种情况下,编译器会减少检查?
这是 Roslyn 中的重大更改,记录在案 here:
The definite assignment rules implemented by previous compilers for dynamic expressions allowed some cases of code that could result in variables being read that are not definitely assigned. See https://github.com/dotnet/roslyn/issues/4509 for one report of this.
[截取示例]
Because of this possibility the compiler must not allow this program to be compiled if val has no initial value. Previous versions of the compiler (prior to VS2015) allowed this program to compile even if val has no initial value. Roslyn now diagnoses this attempt to read a possibly uninitialized variable.