Details of the highly anticipated C# 12 Preview have leaked, giving a sneak peek into upcoming features that should revolutionize the language. These features hold great promise for improving code readability, optimizing performance, and unlocking advanced capabilities of the language.
Improved Switch Expressions
C# 8 introduced basic switch expressions that let you express complex conditional logic concisely, in a readable form. C# 12 takes this further by introducing a new pattern-matching syntax for switch expressions, making it even easier to write expressive and concise code.
Consider the following code snippet.
var result = obj switch
{
int i when i > 0 => "Positive",
int i when i < 0 => "Negative",
_ => "Zero"
};
This code uses switch expressions to determine whether an integer is positive, negative, or zero. In C# 12, you can simplify this code even further using the new pattern-matching syntax:
var result = obj switch
{
> 0 => "Positive",
< 0 => "Negative",
_ => "Zero"
};
This syntax allows you to omit the when keyword and use relational operators directly in the switch expression.
Primary Constructors for Classes and Structs
You can now create primary constructors in any class or struct (a lightweight class alternative). Using primary constructors, you can add parameters to the class declaration and use these values inside the class body.
C# 9 introduced primary constructors as part of the positional syntax for records. C# 12 extends these to all structs and classes.
You can put the parameters after the type name in brackets as shown below:
public class Student(int id, string name, IEnumerable<decimal> grades)
{
public Student(int id, string name) : this(id, name, Enumerable.Empty<decimal>()) { }
public int Id => id;
public string Name { get; set; } = name.Trim();
public decimal GPA => grades.Any() ? grades.Average() : 4.0m;
}
The parameters of a primary constructor are in scope throughout the declaring type's entire body. You can set up properties or fields or can also utilize them in methods or local functions as variables. You can provide these parameters to a base constructor as well.
Interpolated Strings Improvements
Interpolated strings have been around since C# 6. In C# 12, you can now create dynamic values for strings using complicated expressions.
int i = 5;
string output = $"The value of i is {i}, and its square is {i*i}.";
Console.WriteLine(output);
This code prints "The value of i is 5, and its square is 25".
Using Directives for Additional Types
With C# 12, you can use the using alias directive to alias any type, not just named types. You can create semantic aliases for tuples, arrays, pointers, or other unsafe types.
Here are a few examples:
using Measurement = (string Units, int Distance);
using UnitsInt = int?;
Aliases usage example:
public void Calculation(Measurement measurement)
{
// Method Body
}
Lambda Expression Improvements
C# 12 empowers lambda expressions by allowing you to define the default values for parameters. The syntax is identical to that of other default parameters:
For example, (int incrementTo = 5) => incrementTo + 1 sets a default value of 5 for the incrementTo parameter, which the lambda call will use if you don’t pass it a value.
var incrementWithDefault = (int incrementTo = 5) => incrementTo + 1;
incrementWithDefault(); // 6
incrementWithDefault(9); // 10
Besides that, many other enhancements came to lambda expressions to make them more effective.
For example:
- You can now create more complex expressions within lambda functions.
- You can now transform lambda expressions into expression trees that simplify complex queries and optimize performance.
Async Streams
You can iterate through asynchronous data sources with the new async streams feature of C# 12. This new iterator—await foreach—helps you iterate over a set of async data:
await foreach (var item in GetItemsAsync())
{
Console.WriteLine(item.value);
}
C# 12: Revolutionizing Code With Exciting New Features
In anticipation of C# 12, you can expect a game-changing update with exciting new features. These advances, such as enhanced switch expressions, primary constructors, and improvements to lambda expressions, indicate a strong focus on code simplicity, flexibility, and performance.
C# 12 is poised to deliver a significant leap forward, enabling you to write more expressive, efficient, and powerful code.