Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences in the new syntax of C# 6.0 / New null check syntax?

First of all, I apologize for title used in this question, since I don't even know how to ask it nor how to search for it, so it may be there, somewhere, a question similar to this one.


I recently stumped upon a Potential Fix on Visual Studio 2015 that I've never seen before. When building EventHandlers, I usually do something like this:

public event EventHandler MyEvent;

internal void onMyEvent(EventArgs eventArgs) {
    if(MyEvent != null) {
        MyEvent(this, eventArgs);
    }
}

But now, Visual Studio 2015 grays out the if statement and corresponding brackets and, when hovered, show a message saying Delegate invocation can be simplified. The simplified version would look something like this:

public event EventHandler MyEvent;

internal void onMyEvent(EventArgs eventArgs) {
    MyEvent?.Invoke(this, eventArgs);
}

How does this exactly work? Wouldn't an Exception be thrown due to be invoking a method out of null or the code stops executing if the expression before '?' is null?

Also, could I replace every if statement that checks if a variable / method / method is null with the '?', putting aside the readability of the code?


Side note: I'm assuming this is from the new version of the C#, 6.0, because I've never seen it before. Please, correct me if I'm wrong

like image 334
auhmaan Avatar asked Apr 27 '16 09:04

auhmaan


2 Answers

As it was alreay pointed out this C#6 feature is called Null-conditional Operators.

It's also worths looking into the generated C# code with ILSpy:

internal void onMyEvent(EventArgs eventArgs)
{
    EventHandler expr_07 = this.MyEvent;
    if (expr_07 != null)
    {
        expr_07(this, eventArgs);
    }
}

As you see this is a pure compiler feature.. it just rewrites your code to the traditional null-check.

To answer you question (although I guess you already figured this out by looking at the code):

Wouldn't an Exception be thrown due to be invoking a method out of null or the code stops executing if the expression before '?' is null?

There won't be an exception thrown when your eventhandler is null. The code just won't be executed when an object before the ? is null.

Now until this point this was only a method call. Obviously if you want to assign a value which is a result of an expression with a Null-conditional Operator it is a little bit more complicated: If it is a reference type then your variable remains null, but if it would be a value type then it will be automatically nullable and will be also null if the the object on which you use the '?' operator is null.

Here is a de-compiled code for this case:

Program.Customer[] array = new Program.Customer[10];
    if (array == null)
    {
        int? arg_33_0 = null;
    }
    else
    {
        Program.Customer expr_1A = array[0];
        if (expr_1A == null)
        {
            int? arg_33_0 = null;
        }
        else
        {
            new int?(expr_1A.IntField);
        }
    }
like image 163
gregkalapos Avatar answered Sep 20 '22 22:09

gregkalapos


Making it simple...

The ? in MyEvent?.Invoke(this, eventArgs); it's called Null Conditional Operator and it's used to simplify the code.

They're used to test for null before calling a method or accessing the index of a enumerable, such an Array or List.

This example is found on the page about the Null Condition Operator and it's self explanatory:

// null if customers is null
int? length = customers?.Length;

// null if customers is null
Customer first = customers?[0];

// null if customers, the first customer, or Orders is null
int? count = customers?[0]?.Orders?.Count();
like image 34
auhmaan Avatar answered Sep 18 '22 22:09

auhmaan