Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 6 null conditional operator does not work for LINQ query

Tags:

c#

linq

roslyn

I expected this to work, but apparently the way the IL generates, it throws NullReferenceException. Why can't the compiler generate similar code for queries?

In the ThisWorks case, the compiler generates code that short circuits the rest of the expression, why can't it do the same thing for LINQ query case?

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

Decompiled results

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;
}
like image 485
Joseph Kingry Avatar asked Mar 09 '16 20:03

Joseph Kingry


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Is C or C++ same?

While C and C++ may sound similar, their features and usage differ. C is a procedural programming language that support objects and classes. On the other hand C++ is an enhanced version of C programming with object-oriented programming support.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

The answer is in the C# language specification, which says

A query expression of the form

from x in e select x

is translated into

( e ) . Select ( x => x )

Note the parentheses around e in the last line. That shows clearly that the null-conditional expression (in your example) ends before Select is called, which means Select might be called with the resulting null.

Why can't it do the same thing for Linq? Because that's not the way the feature was designed to work. The specification for the null-conditional operators do not have a special case for queries, nor vice versa.

like image 161
Neal Gafter Avatar answered Oct 13 '22 17:10

Neal Gafter