Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท .NET, Tools and Concepts

What is new in C# 12

A feature tour of C# 12 on .NET 8: primary constructors for classes, collection expressions, alias any type, inline arrays, default lambda parameters and ref readonly.

C# 12 shipped in November 2023 with .NET 8, a long-term-support release. It is a "convenience" release: most of its features remove boilerplate you wrote in every project, such as constructors that only copy parameters into fields, and the many different ways of creating a list, an array or a span.

Every example below compiles with the C# 14 compiler used on this site, which accepts all C# 12 code. To use the features in your own project, target net8.0 or newer, or set <LangVersion>12</LangVersion>.

C#
public class Counter(int start)
{
    private int _value = start;
    public int Next() => _value++;
}

int[] numbers = [1, 2, 3];

Try it

C# Primary constructors and collection expressions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Alias any type, default lambda parameters, ref readonly, inline arrays

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Feature tour

Primary constructors for classes and structs

Records already had primary constructors. C# 12 lets any class or struct declare constructor parameters right after its name. The parameters are in scope in the whole type body: in field initializers, methods and property initializers. They are not properties and they are not public. If a member uses a parameter, the compiler captures it in a hidden field.

Use them for dependency injection and simple data holders. Keep in mind that the captured parameter is a mutable field, unlike a record's init property. To validate a parameter, assign it in a field initializer such as private readonly string _name = name ?? throw new ArgumentNullException(nameof(name));. See Primary constructors.

Collection expressions

A collection expression is a list of elements in square brackets. It has no type of its own: the type it converts to decides what is created. Arrays, List<T>, Span<T>, ReadOnlySpan<T>, IEnumerable<T>, IReadOnlyList<T>, ImmutableArray<T> and your own types with a collection builder all work. The spread element ..x inserts every element of another collection. [] is the empty collection of whatever type is expected. See Collection expressions.

Alias any type

A using alias could previously name only a named type. C# 12 allows tuples, arrays, pointers and other constructed types, which is handy for a name you reuse across a file. In C# 12 an alias is still limited to a single file; combine it with global using to share it across a project.

Default values for lambda parameters

Lambdas can now have default values and params arrays, like methods. The compiler infers a custom delegate type for them, so use var as in the example. A lambda assigned to Func<int, int, int> cannot use a default, because Func has no defaults.

ref readonly parameters

in parameters accept a value without writing in at the call site, which hid the fact that a reference was passed. ref readonly makes the intent explicit: the call site should use ref or in, and the method promises not to change the argument. Passing a plain variable without a modifier produces a warning, and passing an rvalue (a temporary) also warns.

Inline arrays

An [InlineArray(N)] struct holds N elements of its single field's type directly inside the struct, with no separate heap object. They are mainly for library authors and performance-sensitive code. A struct with this attribute can be indexed, enumerated with foreach and converted to Span<T>.

Other additions

  • Experimental attribute: [Experimental("DIAGID")] marks types and members so that using them produces a compiler error until the caller suppresses that diagnostic.
  • Interceptors are experimental (they require opting in) and are designed for source generators. They are not covered by the language's stability promise.

Usage notes

  • Collection expressions are not always the cheapest. A target-typed IEnumerable<T> may allocate an array or a list; use T[] or ReadOnlySpan<T> when you want to control that.
  • Spread needs an enumerable or a span. [.. a, .. b] copies the elements; it does not alias them.
  • A primary constructor parameter shadows a member with the same name in some places. The compiler warns (CS9124 and friends) if you both capture a parameter and use it to initialize a member, because two copies can drift apart.
  • Records are different: record primary-constructor parameters become public init-only properties, class and struct parameters do not.
  • Aliases need using before any type. Place using X = ...; directives at the top of the file (or use global using).
  • Check the target framework. Inline arrays and collection builders use new runtime types, so they need net8.0 or later.