?? and ?: -- Somewhat lesser known operators

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    ?? and ?: -- Somewhat lesser known operators

    I've known about these beauties for quite some time, but never thought of sharing them until now. They're a bit obscure, and I want to help remedy that.
    =============== =============== =============== ==========

    ?? is a C# binary operator used to provide a default value should the left operand be null. For example:

    Code:
    X = A ?? B;
    equates to:
    Code:
    if(A != null)
      X = A;
    else
      X = B;
    Relatively simple, but a great time saver, and sometimes quite useful. Another example:
    Code:
    string result = ( TextBox1 ?? new TextBox() ).Text;
    In this example, if TextBox1 is not null, result will contain TextBox1.Text. Otherwise, it will contain a blank string, since that is what new TextBoxes are initialized to.
    =============== =============== =============== ==========

    ?: is a C# (and many other languages) ternary operator used to return a value based on the evaluation of a boolean expression. This one is far more commonly known than the ?? operator, because it has its roots in C, but I'm still surprised at how many people don't know how to use it. Here's an example of how it works:

    Code:
    X = A ? B : C;
    This is equivalent to:
    Code:
    if(A)
      X = B;
    else
      X = C;
    A more practical example:
    Code:
    string substr = fullstr.Length > 10 ? fullstr.Substring(0, 10) : fullstr;
    This code sets substr's value to the first 10 characters of fullstr, unless fullstr is shorter than 10 characters, in which case, substr is assigned the value of fullstr.
    =============== =============== =============== ==========

    Hope you found this enlightening!
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    Cool, i didn't knew about the first one ??

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Yeah, the first one is new on me, and very useful.

      Note on the 2nd one:
      X = A ? B : C;
      A can be expanded to compound boolean statements but you need to wrap the whole package in ( )
      X= (Aa && Ab)? B : C;

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        I knew the inline-if statement was pretty well known, if less used, because it's been around since C, if not before.

        But the null coalescing operator is practically unknown, and it's great.

        Comment

        • Bassem
          Contributor
          • Dec 2008
          • 344

          #5
          Nice topic. ?? is new to me. Good to add that
          The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type. If you try to assign a nullable value type to a non-nullable value type without using the ?? operator, you will generate a compile-time error.
          From the link that insertAlias provided.

          Comment

          Working...