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. C# 9.0: Target-typed new expressions – Make your initialization code less verbose

C# 9.0: Target-typed new expressions – Make your initialization code less verbose

Scheduled Pinned Locked Moved The Insider News
csharpcsscom
7 Posts 7 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.
  • K Offline
    K Offline
    Kent Sharkey
    wrote on last edited by
    #1

    Thomas Claudius Huber[^]:

    In this blog post, let’s look at another very interesting feature of C# 9.0 that is called target-typed new expressions.

    More on the new new, for those that don't knew (sorry, wanted it to rhyme)

    S B D Richard Andrew x64R D 5 Replies Last reply
    0
    • K Kent Sharkey

      Thomas Claudius Huber[^]:

      In this blog post, let’s look at another very interesting feature of C# 9.0 that is called target-typed new expressions.

      More on the new new, for those that don't knew (sorry, wanted it to rhyme)

      S Offline
      S Offline
      Super Lloyd
      wrote on last edited by
      #2

      Mmm....

      Friend friend = new();

      Are those initialiser for people who have existential crisis at the mere sight of a var keyword? possible javascript trauma, I reckon...

      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

      1 Reply Last reply
      0
      • K Kent Sharkey

        Thomas Claudius Huber[^]:

        In this blog post, let’s look at another very interesting feature of C# 9.0 that is called target-typed new expressions.

        More on the new new, for those that don't knew (sorry, wanted it to rhyme)

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

        Quote:

        When reading the code, you need to look at the types of the properties AddFriendCommand and Friends to know what the two new expressions in the code snippet above are actually creating. But that’s only true if you do not read the code in a tool like Visual Studio, but in a simple text editor or on a blog like this one. If you look at the code in Visual Studio, you can hover any target-typed new expression with your mouse to see which constructor it calls. In the screenshot below I hovered the second new expression in Visual Studio and you can see in the tooltip that it actually creates an ObservableCollection.

        Huber is a good writer. Type inference on the left (var) now joins inference on the right (new(#####)): I feel dyslexic !

        «One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

        1 Reply Last reply
        0
        • K Kent Sharkey

          Thomas Claudius Huber[^]:

          In this blog post, let’s look at another very interesting feature of C# 9.0 that is called target-typed new expressions.

          More on the new new, for those that don't knew (sorry, wanted it to rhyme)

          D Offline
          D Offline
          Dan Neely
          wrote on last edited by
          #4

          I'll give this article credit for actually showing a case where this isn't just a case of moving the type name from right to left but actually allows you to omit the type when you couldn't before.

          //old
          myObject.Friend = new Friend("Bob");

          //new
          myObject.Friend = new("Bob");

          Did you ever see history portrayed as an old man with a wise brow and pulseless heart, weighing all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt

          R 1 Reply Last reply
          0
          • K Kent Sharkey

            Thomas Claudius Huber[^]:

            In this blog post, let’s look at another very interesting feature of C# 9.0 that is called target-typed new expressions.

            More on the new new, for those that don't knew (sorry, wanted it to rhyme)

            Richard Andrew x64R Offline
            Richard Andrew x64R Offline
            Richard Andrew x64
            wrote on last edited by
            #5

            This isn't really all that much. It's just syntactic shorthand. The same IL code is generated, I'm sure.

            The difficult we do right away... ...the impossible takes slightly longer.

            1 Reply Last reply
            0
            • K Kent Sharkey

              Thomas Claudius Huber[^]:

              In this blog post, let’s look at another very interesting feature of C# 9.0 that is called target-typed new expressions.

              More on the new new, for those that don't knew (sorry, wanted it to rhyme)

              D Offline
              D Offline
              Dominic Burford
              wrote on last edited by
              #6

              This is just syntactic sugar and doesn't really add very much. I actually prefer the more verbose version of instantiating an object as it's less ambiguous.

              "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." - C.A.R. Hoare Home | LinkedIn | Google+ | Twitter

              1 Reply Last reply
              0
              • D Dan Neely

                I'll give this article credit for actually showing a case where this isn't just a case of moving the type name from right to left but actually allows you to omit the type when you couldn't before.

                //old
                myObject.Friend = new Friend("Bob");

                //new
                myObject.Friend = new("Bob");

                Did you ever see history portrayed as an old man with a wise brow and pulseless heart, weighing all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt

                R Offline
                R Offline
                Rob Grainger
                wrote on last edited by
                #7

                Personally, I'm not a fan. Generally, I'm all in favour of type inference, but in this case it seems to actually reduce readability. In the example above, its not possible to know the type of object being created without looking at the definition of myObject.Friend. Unlike the usual case using `var`, where the type is generally obvious from the right-hand side.

                "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                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