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