Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the .NET Reflector unable to reflect over the null-coalescing operator correctly?

I wrote this piece of code:

private Queue<int> EnsureQueue()
{
    return _queue ?? (_queue = new Queue<int>(10));
}

and the reflector gives me:

private Queue<int> EnsureQueue()
{
    if (this._queue == null)
    {
    }
    return (this._queue = new Queue<int>(10));
}

Obviously, this is not what the original code says. The line (this._queue = new Queue<int>(10)); will alway return a new Queue<int>(10) instead of _queue when it is not null.

Is this a bug in the .NET Reflector or am I missing something? The program seems to behave correctly...

EDIT -> See my answer

like image 229
HerpDerpington Avatar asked Oct 20 '22 02:10

HerpDerpington


1 Answers

This is what my copy of Reflector makes of this method:

private Queue<int> EnsureQueue()
{
    return (this._queue ?? (this._queue = new Queue<int>(10)));
}

Looks pretty darn good to me. Version 8.5.0.179, be sure to update yours.

like image 68
Hans Passant Avatar answered Nov 15 '22 04:11

Hans Passant