Coffeehouse Thread

14 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

C# trivia question

Back to Forum: Coffeehouse
  • User profile image
    cbae

    How would you rewrite this line of code without using the ?? operator and achieve functional equivalence (in terms of side effects)?

    var someval = SomeMethod() ?? String.Empty;

     

  • User profile image
    JoshRoss

    // you wouldn't want to eval twice.
    var test = SomeMethod();
    var someval = test != null ? test : String.Empty;
    // or would you?
    

    -Josh

  • User profile image
    Richard.Hein

    @cbae: Maybe monad.

  • User profile image
    cbae

    @JoshRoss: Yup. That is absolutely correct.

    ?? is a beautiful thing for that reason, yet I still this kind of code all the time:

    var someval = (SomeMethod() == null) ? String.Empty : SomeMethod();
    

     

  • User profile image
    Proton2

    I have never heard of this operator... and now I have Smiley

    From:

    http://stackoverflow.com/questions/446835/what-do-two-question-marks-together-mean-in-c

     

    It's the null coalescing operator, and quite like the ternary (immediate-if) operator.

    FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();

     

    expands to:

    FormsAuth = formsAuth != null ? formsAuth : new FormsAuthenticationWrapper();

     

    which further expands to:

    if(formsAuth != null)

        FormsAuth = formsAuth;

    else

        FormsAuth = new FormsAuthenticationWrapper();

     

    In English, it means "If whatever is to the left is not null, use that, otherwise use what's to the right."

    Note that you can use any number of these in sequence. The following statement will assign the first non-null Answer# to Answer:

    string Answer = Answer1 ?? Answer2 ?? Answer3 ?? Answer4;

  • User profile image
    ManipUni

    Why have I never seen that before? It isn't like I'm new to C#.   

    I guess I need to catch up on all the changes...

  • User profile image
    spivonious

    That's a new operator to me too. It looks a lot like Oracle's NVL function.

    SELECT NVL(some_col, 'null') FROM DUAL;

  • User profile image
    cbae

    I've been using this operator for a while, but I always thought it was syntactic sugar. Yesterday, I decided run some code through the debugger, and I was surprised to find the difference between them.

    This will invoke SomeMethod() twice:

    var someval = (SomeMethod() == null) ? String.Empty : SomeMethod();

    This will not:

    var someval = SomeMethod() ?? String.Empty;

     

  • User profile image
    DeathBy​VisualStudio

    Resharper taught me this trick some time ago. ?? is one of my best friends. I highly recommend having a good refactoring tool like Resharper riding shotgun as it can show you little gems like this.

    If we all believed in unicorns and fairies the world would be a better place.
    Last modified
  • User profile image
    Dr Herbie

    @cbae: I hadn't thought of that! 

    I've been using ?? since I started using .NET3.5 -- it's like the T-SQL  ISNULL(field, defaultvalue) function.

    I just assumed everyone would know about it; perhaps I should add this to our list of developer interview questions.

    Herbie

     

  • User profile image
    itsnotabug

    I saw ?? in a code sample from a vendor about a year ago and thought I had encoding issues... now I use it all the time. In the 1.1/2.0 days I had a crappy utility library of NoZed() functions dedicated for this.

  • User profile image
    magicalclick

    I leared this like last week. But, can the mothod return any object type with null value? Can you assign either myObject(null) and string(null) in this statement? Will it raise compiler error? I am going to try that out.

    update: doesn't compile, which is good.

    Leaving WM on 5/2018 if no apps, no dedicated billboards where I drive, no Store name.
    Last modified
  • User profile image
    DeathBy​VisualStudio

    I work with guys I've shown things like ?? to but they still like writing statements like:

    if (isDirty != null && isDirty == true){
         Save()
    }
    

    Forget the adoption of ??; comparing a Boolean? They say that it's more readable. Ug...

    (Ok, a nullable Boolean is a bad example but you get what I mean.)

    If we all believed in unicorns and fairies the world would be a better place.
    Last modified
  • User profile image
    cbae

    , DeathByVisualStudio wrote

    I work with guys I've shown things like ?? to but they still like writing statements like:

    1
    2
    3
    if (isDirty != null && isDirty == true){
         Save()
    }

    Forget the adoption of ??; comparing a Boolean? They say that it's more readable. Ug...

    (Ok, a nullable Boolean is a bad example but you get what I mean.)

    Ugh is right. Although if you didn't know what ?? does, this might be a little confusing to some:

    if (isDirty ?? false){     Save()}

    But that is when curiosity should lead you to find out WTF the ?? operator is for, and then you might actually learn something.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.