#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


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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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