Skip to content
Tutorials Center
My learning

Topics

Lesson 94 of 182 ยท Pattern Matching and Modern C#

C# 14 Features in Depth

A hands-on tour of C# 14 on .NET 10 - extension members, the field keyword, null-conditional assignment, span conversions, partial constructors, lambda modifiers and more.

C# 14 ships with .NET 10, the long-term-support release this tutorial runs on. Unlike C# 9 or C# 12, it is not one big headline feature but a set of focused improvements that remove small annoyances: a backing field you had to declare by hand, a null check you had to wrap around an assignment, a lambda whose parameters needed full type names only because one was out. Together they make everyday code shorter without making it harder to read.

The previous lesson, What's New in C# 8 to 14, gave a one-line summary of each version. Here we slow down and look at every C# 14 feature with a runnable example, what problem it solves, and where it can surprise you. Every example on this page was compiled and run with the C# 14 compiler of the .NET 10 SDK. You do not need any setting to switch the features on in our editor: C# 14 is the default language version for .NET 10 projects.

The feature list is: extension members, the field keyword, null-conditional assignment, implicit span conversions, nameof with unbound generic types, lambda parameter modifiers without types, partial constructors and events, and user-defined compound assignment operators. We take them in that order, roughly from most to least common in everyday code.

Extension members

Classic extension methods, which you met in Extension Methods and Extension Members, could only add methods, and only instance-style ones. C# 14 introduces extension blocks. Inside a block you declare the receiver once, then write members that look like they belong to the receiver's type: methods, properties, even static members and operators.

C# Extension properties and static extension members

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.

Read the block as "for receivers of type IEnumerable<T>, here are some extra members". IsEmpty is an extension property: callers write words.IsEmpty with no parentheses. extension(string) has no receiver name, because it contains only static members: the call string.Repeated(...) looks like a static method of string. The generic parameter <T> is declared on the block instead of on every method.

Extension members are still ordinary static code underneath. They cannot read private state, they add no fields, and an instance member of the real type always wins. The full rules are in the extension lesson. The old this syntax keeps working and you can mix both in one class, so you only move to blocks when you need a property or a static member.

The field keyword

Auto-properties are great until you need one small rule, for example trimming a string on assignment. Before C# 14 that forced you to throw away the auto-property, declare a private field, and write both accessors. The field keyword gives you the compiler-generated backing field inside an accessor.

C# field: custom logic without a manual backing field

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.

Inside Name, field is a hidden private string. In Score, only the setter has a body, so the getter stays automatic (get;) and the compiler still creates the field. Notice that the object initializer Score = 250 runs the setter, so 250 is clamped to 100.

Two cautions. First, field is a contextual keyword: if your class already has a member or variable called field, inside an accessor it now refers to the backing field. The compiler warns about that situation, and you can write @field or this.field to mean your own member. Second, the compiler still applies nullable analysis to the hidden field. The Name property above ends with = "anonymous" (a property initializer, which is allowed with field) so the field is never null; without it the compiler warns CS9264: Non-nullable property 'Name' must contain a non-null value when exiting constructor. See Properties for the basics.

Null-conditional assignment

You know customer?.Name to read through a possibly null reference. C# 14 lets the same operators appear on the left of an assignment. The right-hand side is evaluated only if the receiver is not null.

C# Assigning through ?. and ?[]

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.

When numbers is null, Expensive("skipped") is never even called: the whole assignment is short-circuited, exactly like a ?. call chain. Compound assignments such as += work too. What you cannot do is use ++ or -- through ?., and the result of such an assignment is not a value you can use, so var x = (a?.B = 1); is an error. It is a statement for its side effect.

Use it for optional objects you are happy to ignore when absent. If a missing object is really a bug, a plain assignment that throws is better than silently doing nothing.

Implicit span conversions

Span<T> and ReadOnlySpan<T> are views over memory (see Span and Memory later in the track). Before C# 14, passing an array or a string to a method that wanted a span worked in simple cases, but extension methods and generic inference often failed to see the conversion. C# 14 makes spans "first-class": arrays convert implicitly to Span<T> and ReadOnlySpan<T>, Span<T> converts to ReadOnlySpan<T>, and these conversions take part in extension method lookup and type inference.

C# Arrays as spans, including extension calls

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.

The interesting line is data.SumOfEvens(): data is an int[], the extension wants a ReadOnlySpan<int>, and C# 14 now finds the method. On older compilers you had to write data.AsSpan().SumOfEvens(). The practical effect is that span-based APIs feel as natural as array-based ones. A side effect is that overload resolution can pick a span overload where it used to pick an array or IEnumerable<T> one, which is usually faster and almost always the same result.

nameof with unbound generic types

nameof(List<int>) used to be the only way to name a generic type, and it dragged in a meaningless type argument. C# 14 accepts an unbound generic type.

C# nameof with unbound generic types

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.

All of these print the plain identifier without type arguments, which is what you want in log messages and argument-exception text. It is a small feature but removes a pointless <object> from many codebases.

Lambda parameters with modifiers, but no types

Lambdas let you leave out parameter types: (a, b) => a + b. That stopped working the moment a parameter needed ref, out, in, scoped or ref readonly, because the modifier forced you to spell out the type too. In C# 14 the modifier can stand alone as long as the target delegate type supplies the types.

C# Lambdas with out and ref but no parameter types

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.

The compiler reads TryParse<int> and knows that text is a string and result is an int. Note the one exception: the params modifier still needs an explicit type, because it changes how the parameter is declared, not only how it is passed.

Partial constructors and partial events

Partial classes let a generator and a human write one type in two files. Methods and, since C# 13, properties could be partial. C# 14 adds constructors and events: one part declares the signature, the other implements it. This is mostly for source generators (for example, a generator that wires up an event or a base-constructor call), but you can see the mechanics in a single file.

C# A partial constructor and a partial event

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.

Rules to remember: the defining declaration of a constructor has no body, the implementing one has a body, and only the implementing part may contain a constructor initializer (: this(...) or : base(...)) and, for a primary-constructor class, only one part may use primary-constructor syntax. A partial event's defining part looks like a field-like event; the implementing part must provide explicit add and remove.

User-defined compound assignment operators

You already know operator + from Operator Overloading. When you write a += b on a type, C# computes a = a + b, which creates a new object. For big mutable types (a matrix, a big buffer, a builder) that copy is wasteful. C# 14 lets you define operator += directly, and it mutates the existing instance.

C# Defining operator += to update in place

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.

The instance operator has a void return type, is not static, and has a single parameter. bag += 5 calls it and, because it mutates the existing object, alias sees the change and ReferenceEquals is true. Without the += operator the compiler would have used the static +, and bag would point at a new object. Supported forms are the compound versions of the arithmetic, bitwise and shift operators, plus ++ and --. Only define them when in-place mutation is part of the type's contract, and keep them consistent with the plain operator: a += b should leave a equal to what a + b would have been.

Checking which language version you are on

If a feature in this lesson fails to compile in your project with an error such as CS8370: Feature 'extension' is not available in C# 13, the project is targeting an older language version. In your own project file you select the version like this (read-only, because our editor always uses the latest):

XML
<PropertyGroup>
  <TargetFramework>net10.0</TargetFramework>
  <LangVersion>14</LangVersion>
</PropertyGroup>

With net10.0 as the target the default is already 14. Libraries that must also compile for net8.0 can still use the new syntax, since most of these features are compiler-only, but extension blocks that rely on new base-library APIs need the matching runtime.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the field keyword refer to inside a property accessor?

    field is the hidden backing field the compiler creates for a property that uses it. The assigned value is called value.
  2. 2. What happens to the right-hand side in numbers?[0] = Expensive(); when numbers is null?

    Null-conditional assignment short-circuits. If the receiver is null, neither the assignment nor the right-hand side runs.
  3. 3. Which of these can an extension block add that a classic this extension method cannot?

    Extension blocks allow properties and static members. They still cannot add state or access private members.
  4. 4. What does nameof(Dictionary<,>) return in C# 14?

    Unbound generic types are allowed in nameof, and the result is the plain identifier without type arguments.
  5. 5. Why define public void operator +=(int amount) instead of relying on operator +?

    Without it, a += b means a = a + b, which creates a new object. The compound operator mutates in place.

Summary

  • C# 14 (default for .NET 10) adds extension blocks with properties and static members, the field keyword, null-conditional assignment, first-class span conversions, unbound nameof, typeless lambda modifiers, partial constructors and events, and user-defined compound assignment.
  • field removes the need for a manual backing field when only one accessor has custom logic.
  • a?.B = value and a?[i] = value skip the whole assignment, including the right-hand side, when a is null.
  • Compound assignment operators let big mutable types update in place; keep them consistent with the plain operators.
  • Most of these features are compiler-only, so they work on older target frameworks if you raise LangVersion.

Next: Threads