🏠 VisualStudioTutor.com  ·  C# Tutorial Home  ·  C# Lesson 28 of 40
Lesson 28 of 40 C# 14 Advanced ⏱ 35 min

C# 14 & Modern Syntax — Primary Constructors, field & Null Safety

Combine modern C# syntax with genuine C# 14 features. Primary constructors and collection expressions arrived earlier; C# 14 adds features such as field-backed properties and null-conditional assignment.

Part 1: What You Will Learn

Combine modern C# syntax with genuine C# 14 features. Primary constructors and collection expressions arrived earlier; C# 14 adds features such as field-backed properties and null-conditional assignment.

  • Use a primary constructor to capture constructor parameters in a concise class declaration.
  • Use a collection expression for compact collection initialization.
  • Use the C# 14 `field` keyword to validate an auto-property without declaring a backing field.
  • Use C# 14 null-conditional assignment to update an object only when it is non-null.

Project setup: Create a .NET 10 Console App using C# 14. Visual Studio 2026 with the .NET 10 SDK supports C# 14.

Part 2: Topic-Specific Working Example

The following example is written specifically for this lesson. Create the project described above, enter the code, run it, and then change some values to observe how the feature behaves.

// Primary constructors were introduced before C# 14,
// but remain useful modern C# syntax.
public sealed class Product(string name, decimal price)
{
    public string Name { get; } = name;

    public decimal Price
    {
        get;
        set => field = value >= 0
            ? value
            : throw new ArgumentOutOfRangeException(nameof(value));
    } = price;

    public DateTime? LastViewed { get; set; }
}

Product keyboard = new("Keyboard", 299.90m);

// Collection expression (modern C# syntax).
Product[] products =
[
    keyboard,
    new Product("Mouse", 99.90m)
];

// C# 14: null-conditional assignment.
Product? selected = products.FirstOrDefault(p => p.Name == "Keyboard");
selected?.LastViewed = DateTime.UtcNow;

foreach (Product product in products)
{
    Console.WriteLine($"{product.Name}: {product.Price:C}");
}

Part 3: How the Code Works

  • The primary-constructor parameters `name` and `price` are available in instance-member initializers.
  • `field` refers to the compiler-generated backing field for `Price`, so validation can be written without declaring `_price`.
  • `selected?.LastViewed = ...` is a C# 14 null-conditional assignment: the right side is used only when `selected` is not null.
  • Primary constructors and collection expressions are useful modern syntax but are not new C# 14 features; C# 14 itself also adds extension members, span conversions, partial constructors/events, and other language improvements.

Part 4: Mini Project & Practice

Mini project: create a Student primary-constructor class, validate Mark with the `field` keyword, initialize several students with a collection expression, and conditionally set a nullable LastLogin property with `?.`.

Tip: Type the code yourself in Visual Studio 2026, run it, then deliberately change one part at a time. The goal is to understand the feature rather than simply copy the finished example.

When you are comfortable with this lesson, continue to Lesson 29.

C# in Visual Studio 2026

📘 This lesson is part of the book C# in Visual Studio 2026 by Dr. Liew Voon Kiong.

View on Amazon Kindle Edition