Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question mark operator before Invoke method

Discussion in 'Scripting' started by JestemJanem, Apr 1, 2020.

  1. JestemJanem

    JestemJanem

    Joined:
    Sep 2, 2019
    Posts:
    3
    Hello,
    today I've been learning about delegates and events, so to better understand and memorise what I've learned I decided to write a test code for myself if I ever need to use it. Anyway, I've written a method

    Code (CSharp):
    1. proteced virtual void OnTestEvent()
    2. {
    3.      if(DelegateTest != null)
    4.      {
    5.        DelegateTest(this, EventArgs.Empty);
    6.       }
    7. }
    and Visual Studio showed me that my code can be simpler and changed the whole code inside that method into one line:

    Code (CSharp):
    1. DelegateTest?.Invoke(this, EventArgs.Empty);
    2.  
    I've searched the internet, but only found info, that "?" operator can be used as an if statement, but in a different way - to choose between two options if something evaluates to true or false. But nowhere i've found any info about using it like that. Could someone please explain me why, how it works and why it called an Invoke? I would be very thankful :)
     
    HyronXIII likes this.
  2. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    ?. is a null conditional operator. It'll perform the operation only if the operand isn't null. Here it makes sure there are listeners.

    edit : Docs
     
    honor0102, HyronXIII and arfish like this.
  3. JestemJanem

    JestemJanem

    Joined:
    Sep 2, 2019
    Posts:
    3

    Thank you, I understand now :) can you please tell me if it's any better than just checking with an if statement? I've stumbled upon someone saying that it's generally frowned upon to use ? operand, because it can lead to bugs, and it's not neat coding. Is it true?
     
    HyronXIII likes this.
  4. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    It's cleaner and easier to read to me. Personally, I've never had issues with it or heard about related issues. Can't speak for others.
     
    JestemJanem likes this.
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    The only "bug" I can think of resulting from it would be in Unity specifically. Unity has overloaded the "==" operator to return true when compared to 'null' if the object is destroyed. Despite the fact that the managed C# object is NOT null.

    This overload does not work for ?. or ?? operators. So if you write code that accesses anything that inherits from UnityEngine.Object that is destroyed, this operator will continue through, and you'll get the "MissingReferenceException".

    Arguably I would not blame the ?. or ?? operators, but rather Unity and their decision to overload the == operator like that. I know they had debated removing that operator overload... but they decided against it as it would break legacy code.
     
    HyronXIII, JestemJanem and ADNCG like this.
  6. JestemJanem

    JestemJanem

    Joined:
    Sep 2, 2019
    Posts:
    3
    Thank you for your help :)
     
  7. SlimeProphet

    SlimeProphet

    Joined:
    Sep 30, 2019
    Posts:
    50
    I hope someone will correct me if I'm wrong, but it should be okay to use
    ?
    with C# delegates and events because they are system objects.

    Unity's
    ==
    override doesn't check system objects; the point of the override is to combine a true null check (is this reference null) with a Unity-destroyed check (has this Unity object been destroyed via scripting, even if its reference in memory is not yet actually null).

    Could someone more knowledgeable weigh in on this, whether using
    MyEvent?.Invoke()
    should always behave as expected for the reasons I've given? @Baste sorry to at you, but I've seen your name in a lot of these null-check-operator threads... can I ask what you think? This appears to be the only delegate/event-specific thread, and it's a narrow use case that would be pretty handy if viable.
     
    Last edited: Aug 27, 2020
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    Yeah, you're correct, that works as you expect it to. Wrote that exact code today!


    ... unless MyEvent happens to be a UnityEvent (ie. the ones you have on Buttons and whatnot), in which case I'm actually not sure. They do have magic going on with regards to testing if their target has been destroyed, but I'm not sure unityEvent == null does.
     
    SlimeProphet likes this.
  9. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    SlimeProphet likes this.