C# 12 🔍 Discover the Exciting New Features & Improvements 🆕🚀

User Avatar
1 year ago, 5811 views, 2 comments
Cover Image

Several exciting new features come with C# 12 and the.NET 8 preview. C# is easier to use and more effective than previous versions thanks to changes made by the Microsoft development team. In this post, we'll take a quick look at some of the major changes in C# 12.

1. Improved Support for Interpolated Strings 🧩

With the release of C# 12, you now have the option to incorporate expressions inside interpolated strings. With the help of this functionality, You can now create dynamic values for strings using complicated expressions and techniques.

For instance, you can use code like this:

int x = 10;
string message = $"The value of x is {x}, and its square is {x*x}.";

This will result in a string value of "The value of x is 10, and its square is 100."

2. Primary Constructors 📝

You can now create primary constructors in any class and struct. With primary constructors, developers can add parameters to the class declaration and use these values inside the class body.

Primary constructors were introduced in C# 9 as part of the record positional syntax. C# 12 extends these to all structs and classes.

 public class Book(int pageSize, string authorName, IEnumerable<float> prices)
  {
      public Book(int pageSize, string authorName) : this(pageSize, authorName, Enumerable.Empty<float>()) { }
      public int pageSize => pageSize;
      public string authorName { get; set; } = authorName;
      public float totalPrice => prices.Sum();
  }

The good part is that you can avoid the pain of declaring private fields and, you know, binding parameter values to those fields in those tedious constructor bodies by using primary constructors instead.

3. Improved Lambda Expressions 🐑

Many of us are using Lambda expressions in our daily development to gain the code better readability. With this version we see that many enhancements came to lambda expressions make them more effective.
For example, you can now create more complex expressions within lambda functions using the new and , or operators. Besides, lambda expressions can now be transformed into expression trees, simplifying the construction of complex queries and optimizing performance.

4. Async Streams 🌐

You may iterate through asynchronous data sources thanks to the new async streams feature in C# 12. This new iterator await foreach gains us to iterate over a set of async data. See the following code snippet:

await foreach (var record in FetchRecordsAsyncData())
{
   Console.WriteLine(record.text);
}

This will iterate over the asynchronous data returned by the GetAsyncData() method and writes each item's text to the console.

5. Target-typed New Expressions 🎯

Target-typed new expressions, a new feature in C# 12, make it simpler to construct new objects. You can now declare new objects using the var keyword. The object's type is inferred from the context. Especially when dealing with complex types, it gives us better code readability.

An alternative to writing:

AcmeClass thatObject = new AcmeClass();

You can now write:

var thatObject = new AcmeClass();

Conclusion 🎬

As always Microsoft makes developers' life easier with every release of .NET and C#. C# 12 comes with many features and improvements. You now have new tools at your disposal to create more effective, concise, and durable code. You may now design more potent and reliable applications with C# thanks to the increased support for interpolated strings, records, lambda expressions, target-typed new expressions, and async streams.

Start C# 12 with the ABP Framework 📚

ABP Framework offers an opinionated architecture to build enterprise software solutions with ASP.NET Core best practices on top of the .NET and the ASP.NET Core platforms. It is also a powerful infrastructure to help you develop low-effort web-optimized applications. It provides the fundamental web application infrastructure, production-ready dotnet startup templates, modules, asp.net core ui themes, tooling, guides and documentation to implement that ASP.NET core architecture properly and automate the details and repetitive work as much as possible.

If you are starting a new ASP.NET Core project and want a fast website abp.io now...

🆓 It's FREE & OPEN-SOURCE! 🔓

Happy coding 🍿✨


I'm Alper Ebicoglu 🧑🏽‍💻
ABP Framework Core Team Member
Follow me for the latest news about .NET and software development:
📌 twitter.com/alperebicoglu
📌 github.com/ebicoglu
📌 linkedin.com/in/ebicoglu
📌 medium.com/@alperonline

2 comments

Leave Comment
User Avatar
alexanderoliver 9 months ago

With the creation of C# 12 and the .NET eight preview, developers can count on progressed usability and efficiency of their coding obligations. These updates mirror the willpower of the Microsoft development team to decorate the developer revel in and streamline the programming manner. Exploring the new features in C# 12 will absolutely provide insights into how builders can leverage those improvements to jot down cleanser, more create my essay and concise code, and build strong applications. https://bestessaywriter.co.uk/make-my-essay

User Avatar
Navneet@aol.com.au 1 year ago

Many thx for sharing

More From Alper

The new ABP Platform is live!

The new ABP Platform is live! We have unified all abp.io subdomains into abp.io. Docs in one place, easy navigation, modern design and ABP Studio C... Continue Reading

alper July 2024

Announcement: Our New ABP.IO Unified Platform

📢 We are unifying all our sub-domain services under a single platform address 👉 ABP.IO with a new user UI and behavior. Continue Reading

alper March 2024

Top 10 .NET Core Libraries Every Developer Should Know 🔥

This article is intended for .NET Core developers who wish to create a robust and useful.NET core application. It is a list of the most popular and... Continue Reading

alper April 2023

ABP Framework: The Ultimate .NET Web Framework for Rapid Application Development

Are you a .NET developer? There not so many popular ASPNET Framework around. However, the ABP Framework has become popular for many developers due ... Continue Reading

alper April 2023

ABP Framework: Open Source Web Application Development Framework

ABP Framework is an open-source web application development framework that provides developers with a set of tools to build modern, scalable, and m... Continue Reading

alper April 2023