#Net5 – C#9 target-typed new expressions are super cool πŸ†’πŸ†’πŸ†’

Buy Me A Coffee

Hi !

I know I’m late to the party here, however I’m still enjoying some of the new C# 9 features a lot. I just realized that now, I can create objects in a different way, which is still super clear to read.

Disclaimer: some of the new features are cool, however my gut told me that they will make code harder to read. I also understand that I have 20 years of writing C# code in my back, so I need to find the balance here.

As usual, 2 lines of code are the best way to showcase this:

// old school
var pointOldSchool = new System.Drawing.Point(3, 5);

// new school and still very readable
System.Drawing.Point pointCSharpNine = new(3, 5);

// crappy output
Console.WriteLine("Old School: var pointOldSchool = new System.Drawing.Point(3, 5);");
Console.WriteLine(pointOldSchool);

Console.WriteLine("C#9: System.Drawing.Point pointCSharpNine = new(3, 5);");
Console.WriteLine(pointCSharpNine);

This this output as expected:

Old School: var pointOldSchool = new System.Drawing.Point(3, 5);
{X=3,Y=5}
C#9: System.Drawing.Point pointCSharpNine = new(3, 5);
{X=3,Y=5}

I like this new one πŸ‘†

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


References


ΒΏCon ganas de ponerte al dΓ­a?

En Lemoncode te ofrecemos formaciΓ³n online impartida por profesionales que se baten el cobre en consultorΓ­a:

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.