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. Other Discussions
  3. The Insider News
  4. The new ?. Operator in C# 6

The new ?. Operator in C# 6

Scheduled Pinned Locked Moved The Insider News
csharpquestion
9 Posts 8 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 Offline
    C Offline
    cs 09
    wrote on last edited by
    #1

    Quote:

    the code is shorter and the intent is immediately clear with ?. operator

    M P R A M 5 Replies Last reply
    0
    • C cs 09

      Quote:

      the code is shorter and the intent is immediately clear with ?. operator

      M Offline
      M Offline
      Mike Hankey
      wrote on last edited by
      #2

      Need to provide a link

      New version: WinHeist Version 2.1.1 new web site. When you are dead you don't know it, it's only difficult for others. It's the same when you're stupid.

      1 Reply Last reply
      0
      • C cs 09

        Quote:

        the code is shorter and the intent is immediately clear with ?. operator

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Bullshit.

        1 Reply Last reply
        0
        • C cs 09

          Quote:

          the code is shorter and the intent is immediately clear with ?. operator

          R Offline
          R Offline
          Ranjan D
          wrote on last edited by
          #4

          What it has to do with the Insider news?

          Ranjan.D

          1 Reply Last reply
          0
          • C cs 09

            Quote:

            the code is shorter and the intent is immediately clear with ?. operator

            A Offline
            A Offline
            Afzaal Ahmad Zeeshan
            wrote on last edited by
            #5

            Nope, it is not even my favorite update in C# 6; also hiding NullReferenceException is (IMO) not a very great update. My favorite update is the default values for getters and setters. Also maybe you wanted to link the "Quote" text to this hyperlink: http://www.volatileread.com/Wiki?id=2104[^]

            The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

            R 1 Reply Last reply
            0
            • C cs 09

              Quote:

              the code is shorter and the intent is immediately clear with ?. operator

              M Offline
              M Offline
              Marc Clifton
              wrote on last edited by
              #6

              Anybody that writes stuff like this:

              userManager.CurrentUser == null ? null :
              (userManager.CurrentUser.GetRole() == null ? null :
              userManager.CurrentUser.GetRole().Name);

              Or it's .? shorthand, should be summarily shot. If you have to do all these null checks in order to get the name of the role, then you have absolutely done something very very wrong. And in general, whenever I write code that checks for null, the first question I ask myself is, "WTF am I writing this? I better have a damn good reason." Marc

              Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

              P B 2 Replies Last reply
              0
              • M Marc Clifton

                Anybody that writes stuff like this:

                userManager.CurrentUser == null ? null :
                (userManager.CurrentUser.GetRole() == null ? null :
                userManager.CurrentUser.GetRole().Name);

                Or it's .? shorthand, should be summarily shot. If you have to do all these null checks in order to get the name of the role, then you have absolutely done something very very wrong. And in general, whenever I write code that checks for null, the first question I ask myself is, "WTF am I writing this? I better have a damn good reason." Marc

                Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                Unfortunately I have the displeasure of using an API where that would be a minor help. :sigh:

                1 Reply Last reply
                0
                • M Marc Clifton

                  Anybody that writes stuff like this:

                  userManager.CurrentUser == null ? null :
                  (userManager.CurrentUser.GetRole() == null ? null :
                  userManager.CurrentUser.GetRole().Name);

                  Or it's .? shorthand, should be summarily shot. If you have to do all these null checks in order to get the name of the role, then you have absolutely done something very very wrong. And in general, whenever I write code that checks for null, the first question I ask myself is, "WTF am I writing this? I better have a damn good reason." Marc

                  Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

                  B Offline
                  B Offline
                  BillWoodruff
                  wrote on last edited by
                  #8

                  Marc Clifton wrote:

                  "WTF am I writing this? I better have a damn good reason.

                  A "damn good reason" could be that you will have no control over the code/behavior of other people who will use/abuse your code ! Or, your code may access resources/data/feed you have no control over which could be null. I would distinguish between cases where: 1. checking for null is a "benign" requirement, like checking the result of a TryParse call 2. cases where checking for null means something may be "wrong" but you still want to continue for example: when your program depends on an externally supplied object which you have no control over. 3. cases where checking for null is essential because if the result is null then there is something totally "wrong" with the current state, and you do not want to continue. In the third case, I would throw an error. By the way, I recall reading comments on a StackOverFlow thread claiming that using the Ternary operator is extremely slow compared to using 'if/else. See: [^] cheers, Bill

                  «To kill an error's as good a service, sometimes better than, establishing new truth or fact.» Charles Darwin in "Prospero's Precepts"

                  1 Reply Last reply
                  0
                  • A Afzaal Ahmad Zeeshan

                    Nope, it is not even my favorite update in C# 6; also hiding NullReferenceException is (IMO) not a very great update. My favorite update is the default values for getters and setters. Also maybe you wanted to link the "Quote" text to this hyperlink: http://www.volatileread.com/Wiki?id=2104[^]

                    The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                    R Offline
                    R Offline
                    Richard Deeming
                    wrote on last edited by
                    #9

                    Afzaal Ahmad Zeeshan wrote:

                    hiding NullReferenceException is (IMO) not a very great update

                    It's not hiding a NullReferenceException; it's checking each level of the expression for null. In other words, this:

                    DateTime? dateOfBirth = user?.Profile?.DateOfBirth;

                    is equivalent to this:

                    // Look, ma - no exceptions!
                    DateTime? dateOfBirth = user != null && user.Profile != null ? user.Profile.DateOfBirth : default(DateTime?);

                    not this:

                    // Seriously, don't do this!
                    DateTime? dateOfBirth;
                    try
                    {
                    dateOfBirth = user.Profile.DateOfBirth;
                    }
                    catch (NullReferenceException)
                    {
                    dateOfBirth = null;
                    }


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    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