我希望这可以工作,但显然IL生成的方式,它抛出NullReferenceException.为什么编译器不能为查询生成类似的代码?
在这种ThisWorks情况下,编译器生成的代码短路表达式的其余部分,为什么它不能为LINQ查询案例做同样的事情?
class Target
{
public ChildTarget Child;
}
class ChildTarget
{
public int[] Values;
}
IEnumerable<int> ThisWorks(Target target) =>
target.Child?.Values.Select(x => x);
IEnumerable<int> ThisDoesNotWork(Target target) =>
from x in target.Child?.Values select x;
ThisWorks(new Target());
ThisDoesNotWork(new Target()); // this throws NullReferenceException
Run Code Online (Sandbox Code Playgroud)
反编译结果
private static IEnumerable<int> ThisDoesNotWork(Target target)
{
ChildTarget child = target.Child;
IEnumerable<int> values = (child != null) ? child.Values : null;
Func<int, int> func;
if ((func = Program._func) == null)
{
func = (Program._func = new Func<int, int>(Program._funcMethod));
}
return values.Select(func);
}
private static IEnumerable<int> ThisWorks(Target target)
{
ChildTarget child = target.Child;
IEnumerable<int> values;
if (child == null)
{
values = null;
}
else
{
IEnumerable<int> values = child.Values;
Func<int, int> func;
if ((func = Program._func2) == null)
{
func = (Program._func2= new Func<int, int>(Program._funcMethod2));
}
values = values.Select(func);
}
return values;
}
Run Code Online (Sandbox Code Playgroud)
答案是在C#语言规范中,它说
表单的查询表达式
从X在ë选择X
被翻译成
(e).选择(x => x)
注意最后一行中e周围的括号.这清楚地表明,在调用Select之前,null条件表达式(在您的示例中)结束,这意味着可能会使用结果null调用Select.
为什么它不能为Linq做同样的事情?因为这不是该功能的设计方式.空条件运算符的规范没有查询的特殊情况,反之亦然.
| 归档时间: |
|
| 查看次数: |
1268 次 |
| 最近记录: |