C# 6 空条件运算符不适用于 LINQ 查询

标签 c# linq roslyn

我希望它能工作,但显然 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

反编译结果

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;
}

最佳答案

答案在 C# 语言规范中,它说

A query expression of the form

from x in e select x

翻译成

( e ) . Select ( x => x )

请注意最后一行 e 周围的括号。这清楚地表明 null 条件表达式(在您的示例中)在调用 Select 之前结束,这意味着 Select 可能会使用生成的 null 进行调用。

为什么它不能为 Linq 做同样的事情?因为这不是该功能设计的工作方式。空条件运算符的规范没有针对查询的特殊情况,反之亦然。

关于C# 6 空条件运算符不适用于 LINQ 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35902097/

相关文章:

c# - 如何在 C# (linq) 上创建困难组?

c# - 使用智能感知支持从文件动态生成枚举

c# - Entity Framework 6 Oracle 和乐观并发

c# - 在 C# 中编写静态和非静态方法时如何避免 'call is ambiguous...' 错误?

c# - dapper 会用谓词做什么?

c# - 无法找到 Windows.Devices.Enumeration 和 Windows.Storage.Streams

c# - 在带有 LinqDataSource 的 ASP.NET ListView 上,单击搜索按钮时显示数据

c# - LINQ 查询为 Entity DB 中的嵌套对象返回 null

c++ - Roslyn 是否支持 C++ 生成语法树

c# - Roslyn 可以将 C# 编译为 CIL