top of page

C# 12 What's new and upcoming?

Updated: Dec 1, 2023

The latest preview version of c# 12 was released recently, and the official release along with the .Net 8 is approaching "November 2023". Let's look into some of the new features in c# 12.



C# 12 What's new


c#12

Primary Constructor


This latest updated primary constructor can be created for both class and struct type; earlier, this was only restricted to record type. Its scope will be with the entire class. For the remaining constructors to be aligned with the class, they need to call the primary constructor manually with this() syntax.


public class SavingsAccount(string accountID, 
                            string accountHolder, 
                            decimal interestRate)
{
    public SavingsAccount() : this("defaultId", "dummyName", 3.01m) 
    {
        ...
    }
    public decimal CurrentBalance { get; private set; } = 0;

    // Remaining class implementation
}

As you can see in this code snippet above, the class uses a primary constructor with three different parameters, but if we need to create a new constructor, we need to call the primary one with this() syntax.



Spread Operator


The spread operator, .. in a collection expression, replaces its argument with the elements from that collection. The prerequisite is that the argument has to be a collection type. The following examples show how the spread operator works:


int[] firstArray = [1, 2, 3, 4, 5];
int[] secondArray = [6];
int[] thridArray = [7, 8, 9, 10, 11];
int[] combined = [..firstArray, ..secondArray, ..thridArray];
foreach (var item in combined)
{
    Console.Write($"{item}, ");
}
// output:
// 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

As you can see in the output, the final array "combine" contains a single list of items and not the collection of arrays, so basically, the spread operator adds all the items of the initial array individually to the final array.



Inline Array


Inline arrays help create an array of fixed size for a struct type. The size of these arrays is determined at the compile time, which makes it faster because the compiler already knows about the exact layout of it.


Example

[System.Runtime.CompilerServices.InlineArray(10)]
public struct MyInlineArray
{
    private int _element;
}

Usage

var myInlineArray = new MyInlineArray();
for (int i = 0; i < 10; i++)
{
    myInlineArray[i] = i;
}

foreach (var item in myInlineArray)
{
    Console.WriteLine(item);
}


Optional Parameter in Lambda Expression


With C# 12, you can provide default values for parameters on lambda expressions. The syntax and the restrictions on default parameter values are the same as for methods and local functions.


var IncreasedBy = (int input, int factor = 1) => input + factor;

Console.WriteLine(IncreasedBy(100)); // 101
Console.WriteLine(IncreasedBy(100, 7)); // 107

Params can also be used in lambda expressions.

var total = (params int[] values) =>
{
    int total = 0;
    foreach (var item in values) 
    {
        total += item;
    }    
    return total;
};

var sequence = new[] { 10, 20, 30, 40, 50 };
var total = total(sequence);
Console.WriteLine(total); // 150

var empty = total();
Console.WriteLine(empty); // 0


Preview Features

A few features still present in the latest version of this language are under the experimental phase, such as Interceptor and experimental attribute, which may or may not be included in the final release, so we will not be discussing this as of now.









8,253 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page