Here’s What’s New in C# 12

Heads up, .NET Fam! 🚀

Guess what? Microsoft’s been cooking up some coolness in their kitchen, and guess what’s on the menu? Some bomb new features in C# 12 Preview. Let’s break it down, shall we? Buckle up, and let’s roll!

1. Primary Constructors Just Leveled Up! 🚀

If you’re deep in those C# 12 trenches, you gotta hear about this one. Primary constructors aren’t just for records anymore – classes and structs just got invited to the party! 

This feature? Pure hotness. 🌶️ It cuts down the hassle, makes initializing properties a breeze, and just makes everything flow better.

Let’s check out this example:

Let’s break it down:

  • Those constructor parameters (title, author, reviews) are all up in the class, doing their thing.
  • Peep how the Author doesn’t just get the name but keeps it clean, trimming any extra spaces.
  • And that AverageReview? Either gives you the average from the reviews or chills with a calm “No Reviews” if there aren’t any.

One last thing, fam. In these non-record classes and structs, properties aren’t just popping up from primary constructor parameters. So you gotta put in that work and craft ’em manually.

2. Custom Aliases with Using Directives – No More Typing Gymnastics 🤸🏿‍♀️

Okay, let’s jazz things up. C# 12 lets you set up your own cool aliases for types. So instead of long-winded types, you get to use these awesome shortcuts.

Yeah, that’s right! BookDetails in place of that lengthy tuple. Sweet, right?

3. Async Streams – No More Waiting Games 🎮

We all hate waiting, especially when it’s for data. But guess what? With async streams, that’s history!

Fetch your chapters, articles, lyrics – whatever floats your boat – all in real-time.

4. Target-typed Expressions – Keeping it Short and Sassy 💁🏾‍♀️

Last but definitely not least, C# 12 keeps it sleek with target-typed new expressions. Why say more when you can say less?

Old-school way:

The C# 12 swag:

5. Default Lambda Expressions – The Magic Wand 🪄

Lambda expressions just got a dope upgrade! Set default values, and keep your code neat and tidy.

Boom! No rating provided? No worries – it’s got your back with a default.

And That’s the Tea! 🍵

Thank y’all for nerding out on a few of C# 12’s new features with me 🤓. From those leaner constructors to async streams keeping our data on its toes, C# just got a little bit more lit 🔥. 

Craving more? 📚 Jump into the docs – that’s your plug to all things C# 12. 

And hey, if you found this helpful, don’t be a stranger! 🚀 Hit that follow on my socials (Twitter, LinkedIn) and stay in the loop with all the tech chatter, updates, and of course, the gifs!

Leave a Comment

Close Bitnami banner
Bitnami