Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Fave Operator of the Day

Fave Operator of the Day

Scheduled Pinned Locked Moved The Lounge
csharpc++comarchitecturequestion
55 Posts 26 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Chris Maunder

    The C# 2.0 null coalescing operator ?? God bless its little cotton socks.

    cheers, Chris Maunder

    CodeProject.com : C++ MVP

    J Offline
    J Offline
    Jim Crafton
    wrote on last edited by
    #2

    You tease - don't blue-ball us, give up an example!!! :)

    ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog

    N S 2 Replies Last reply
    0
    • J Jim Crafton

      You tease - don't blue-ball us, give up an example!!! :)

      ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #3

      Jim Crafton wrote:

      You tease - don't blue-ball us, give up an example!!!

      return _cachedItem ?? (_cachedItem = GetItem());

      Regards, Nish


      Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
      My latest book : C++/CLI in Action / Amazon.com link

      J L 2 Replies Last reply
      0
      • N Nish Nishant

        Jim Crafton wrote:

        You tease - don't blue-ball us, give up an example!!!

        return _cachedItem ?? (_cachedItem = GetItem());

        Regards, Nish


        Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
        My latest book : C++/CLI in Action / Amazon.com link

        J Offline
        J Offline
        Jim Crafton
        wrote on last edited by
        #4

        Is this just a C# thing or is there a VB equivalent (I'm almost afraid to ask what that monstrosity would look like)?

        ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog

        N S N L 4 Replies Last reply
        0
        • J Jim Crafton

          Is this just a C# thing or is there a VB equivalent (I'm almost afraid to ask what that monstrosity would look like)?

          ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog

          N Offline
          N Offline
          Nish Nishant
          wrote on last edited by
          #5

          Jim Crafton wrote:

          Is this just a C# thing or is there a VB equivalent (I'm almost afraid to ask what that monstrosity would look like)?

          I don't think there's a VB equivalent (though I don't know for sure).

          Regards, Nish


          Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
          My latest book : C++/CLI in Action / Amazon.com link

          1 Reply Last reply
          0
          • C Chris Maunder

            The C# 2.0 null coalescing operator ?? God bless its little cotton socks.

            cheers, Chris Maunder

            CodeProject.com : C++ MVP

            S Offline
            S Offline
            Shog9 0
            wrote on last edited by
            #6

            Personally, i'd have preferred something along the lines of the GNU C "shortcut ternary" operator (which it closely resembles). But then, i'd have liked it if C# interpreted null values as false instead of requiring an explicit comparison... :->

            ----

            ...the wind blows over it and it is gone, and its place remembers it no more...

            C C 2 Replies Last reply
            0
            • J Jim Crafton

              Is this just a C# thing or is there a VB equivalent (I'm almost afraid to ask what that monstrosity would look like)?

              ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog

              S Offline
              S Offline
              Shog9 0
              wrote on last edited by
              #7

              Jim Crafton wrote:

              Is this just a C# thing or is there a VB equivalent (I'm almost afraid to ask what that monstrosity would look like)?

              Monstrosity you say? In VB? Unthinkable! :rolleyes: Classic VB (and VB.NET) has a ternary operator... except, it isn't really an operator. The IIF() function takes three operands, if the first is true it returns the second, otherwise it returns the third. Unlike the C++/C# ternary operator, this will always evaluate all three expressions (being a function call rather than an operator, it has to). This provided yet another pitfall when using VB, as expressions such as: someVar = IIF(boolVar, HorriblyDestructiveCall1(), EvenMoreDestructiveCall()) ...would end up trashing whatever global state you were manipulating with the two functions twice, once for each call (remember, this is VB - of course there's a horrible, fragile, global state of some sort). VB9 now provides a true ternary operator - If. So you can write: someVar = If(boolVar, HorriblyDestructiveCall1(), EvenMoreDestructiveCall()) ... and only one of the possible functions will be called. Or, for the null coalescing version: someVar = If(possiblyNothing, BetterThanNothing()) Good times...

              ----

              ...the wind blows over it and it is gone, and its place remembers it no more...

              C 1 Reply Last reply
              0
              • C Chris Maunder

                The C# 2.0 null coalescing operator ?? God bless its little cotton socks.

                cheers, Chris Maunder

                CodeProject.com : C++ MVP

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #8

                Yeah, I love that one.

                Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                1 Reply Last reply
                0
                • S Shog9 0

                  Personally, i'd have preferred something along the lines of the GNU C "shortcut ternary" operator (which it closely resembles). But then, i'd have liked it if C# interpreted null values as false instead of requiring an explicit comparison... :->

                  ----

                  ...the wind blows over it and it is gone, and its place remembers it no more...

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #9

                  yeah, I still from time to time type if (myobj) and then remember.

                  Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                  B 1 Reply Last reply
                  0
                  • J Jim Crafton

                    Is this just a C# thing or is there a VB equivalent (I'm almost afraid to ask what that monstrosity would look like)?

                    ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog

                    N Offline
                    N Offline
                    NormDroid
                    wrote on last edited by
                    #10

                    Jim Crafton wrote:

                    is there a VB equivalent

                    Who cares ;)

                    WPF - Imagineers Wanted Follow your nose using DoubleAnimationUsingPath

                    B C 2 Replies Last reply
                    0
                    • C Chris Maunder

                      The C# 2.0 null coalescing operator ?? God bless its little cotton socks.

                      cheers, Chris Maunder

                      CodeProject.com : C++ MVP

                      M Offline
                      M Offline
                      M dHatter
                      wrote on last edited by
                      #11

                      NERDS! :cool:

                      M 1 Reply Last reply
                      0
                      • C Chris Maunder

                        The C# 2.0 null coalescing operator ?? God bless its little cotton socks.

                        cheers, Chris Maunder

                        CodeProject.com : C++ MVP

                        L Offline
                        L Offline
                        leppie
                        wrote on last edited by
                        #12

                        jmp Allows you to jump from function to function :)

                        xacc.ide
                        IronScheme a R5RS-compliant Scheme on the DLR
                        The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach."

                        1 Reply Last reply
                        0
                        • N Nish Nishant

                          Jim Crafton wrote:

                          You tease - don't blue-ball us, give up an example!!!

                          return _cachedItem ?? (_cachedItem = GetItem());

                          Regards, Nish


                          Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                          My latest book : C++/CLI in Action / Amazon.com link

                          L Offline
                          L Offline
                          leppie
                          wrote on last edited by
                          #13

                          I would have like a ??= operator too, for exactly what you are showing. Personally, I find it more handy with dealing with optionals.

                          xacc.ide
                          IronScheme a R5RS-compliant Scheme on the DLR
                          The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach."

                          1 Reply Last reply
                          0
                          • M M dHatter

                            NERDS! :cool:

                            M Offline
                            M Offline
                            Mustafa Ismail Mustafa
                            wrote on last edited by
                            #14

                            and proud! :cool:

                            "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook "There is no wealth like knowledge, no poverty like ignorance." Ali ibn Abi Talib "Animadvertistine, ubicumque stes, fumum recta in faciem ferri?"

                            1 Reply Last reply
                            0
                            • J Jim Crafton

                              You tease - don't blue-ball us, give up an example!!! :)

                              ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog

                              S Offline
                              S Offline
                              Steve Hansen
                              wrote on last edited by
                              #15

                              Also easy when working with strings. string result = someString ?? string.Empty;

                              1 Reply Last reply
                              0
                              • C Chris Maunder

                                The C# 2.0 null coalescing operator ?? God bless its little cotton socks.

                                cheers, Chris Maunder

                                CodeProject.com : C++ MVP

                                H Offline
                                H Offline
                                Howard Richards
                                wrote on last edited by
                                #16

                                Aha.. so when is CodeProject being upgrade to 3.5 then ? ;)

                                'Howard

                                1 Reply Last reply
                                0
                                • C Chris Maunder

                                  The C# 2.0 null coalescing operator ?? God bless its little cotton socks.

                                  cheers, Chris Maunder

                                  CodeProject.com : C++ MVP

                                  P Offline
                                  P Offline
                                  Pete OHanlon
                                  wrote on last edited by
                                  #17

                                  For the really nerdy:

                                  string value = value1 ?? value2 ?? value3 ?? finalDefaultValue;
                                  

                                  Deja View - the feeling that you've seen this post before.

                                  My blog | My articles

                                  G L 2 Replies Last reply
                                  0
                                  • P Pete OHanlon

                                    For the really nerdy:

                                    string value = value1 ?? value2 ?? value3 ?? finalDefaultValue;
                                    

                                    Deja View - the feeling that you've seen this post before.

                                    My blog | My articles

                                    G Offline
                                    G Offline
                                    Gary Wheeler
                                    wrote on last edited by
                                    #18

                                    OK. For us poor, ignorant C++ jocks, please explain what that abomination does. I'm guessing it's something like this:

                                    if (value1 != NULL) value = value1;
                                    else if (value2 != NULL) value = value2;
                                    else if (value3 != NULL) value = value3;
                                    else value = finalDefaultValue;


                                    Software Zen: delete this;

                                    P 1 Reply Last reply
                                    0
                                    • G Gary Wheeler

                                      OK. For us poor, ignorant C++ jocks, please explain what that abomination does. I'm guessing it's something like this:

                                      if (value1 != NULL) value = value1;
                                      else if (value2 != NULL) value = value2;
                                      else if (value3 != NULL) value = value3;
                                      else value = finalDefaultValue;


                                      Software Zen: delete this;

                                      P Offline
                                      P Offline
                                      Pete OHanlon
                                      wrote on last edited by
                                      #19

                                      Yup - that's exactly what it does - it coalesces through the chain until it finds a none-null value.

                                      Deja View - the feeling that you've seen this post before.

                                      My blog | My articles

                                      G 1 Reply Last reply
                                      0
                                      • P Pete OHanlon

                                        Yup - that's exactly what it does - it coalesces through the chain until it finds a none-null value.

                                        Deja View - the feeling that you've seen this post before.

                                        My blog | My articles

                                        G Offline
                                        G Offline
                                        Gary Wheeler
                                        wrote on last edited by
                                        #20

                                        I'm curious. Does this pattern occur often enough in .NET programming that it was worth adding an operator for it? Or is it the case that, since the operator is available, you use that pattern more often?


                                        Software Zen: delete this;

                                        D R 2 Replies Last reply
                                        0
                                        • G Gary Wheeler

                                          I'm curious. Does this pattern occur often enough in .NET programming that it was worth adding an operator for it? Or is it the case that, since the operator is available, you use that pattern more often?


                                          Software Zen: delete this;

                                          D Offline
                                          D Offline
                                          Daniel Grunwald
                                          wrote on last edited by
                                          #21

                                          I use ?? from time to time, but I needed more than a single ?? in an expression. I think a "?." operator would be much more useful than ??. "obj?.Method()" could be syntax sugar for "(obj != null) ? obj.Method() : null" (except that "obj" is evaluated only once). Hopefully MS will add something like that to C# 4.0...

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups