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:
public class Book(string title, string author, IEnumerable<string> reviews)
{
public Book(string title, string author) : this(title, author, Enumerable.Empty<string>()) { }
public string Title => title;
public string Author { get; set; } = author.Trim();
public string AverageReview => reviews.Any() ? (reviews.Sum(r => Convert.ToInt32(r)) / reviews.Count()).ToString("F1") : "No Reviews";
}
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.
using BookDetails = (string Title, string Author, int Pages);
using Bookshelf = string[];
using ReviewRating = int?;
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!
await foreach (var chapter in FetchChaptersFromAsyncSource())
{
Console.WriteLine(chapter.title);
}
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:
Book novel = new Book();
The C# 12 swag:
var novel = new Book();
5. Default Lambda Expressions – The Magic Wand 🪄
Lambda expressions just got a dope upgrade! Set default values, and keep your code neat and tidy.
var assignDefaultRating = (int rating = 3) => rating;
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!
Top comments (0)