Modern syntax and language versions
Modern syntax and language versions
Modern C# syntax often expresses an existing operation more concisely, but syntax support depends on the compiler and project language version. Some features also require specific library or runtime capabilities. Keep the target framework, SDK version and language version explicit when sharing examples.
Examples target the proposed C# 14 / .NET 10 console baseline. Each is a separate Program.cs; expected outputs are reasoned, not executed.
Collection expressions have target types
C# 12 introduced collection expressions such as [1, 2] and spreads such as ..other. The target type determines what collection is created. Unlike a lazy iterator, the expression constructs a collection of the included elements. A bare var assignment such as var items = [1, 2]; lacks a target type and is invalid.
using System;int[] classic = new int[] { 2, 4 };int[] modern = [..classic, 6];classic[0] = 9;Console.WriteLine(string.Join(",", modern));Console.WriteLine(string.Join(",", classic));The spread copies the integer values into the new array. This does not imply deep cloning when elements are references.
Expected output (reasoned, not executed):
2,4,69,4Extension methods and extension blocks
An extension method is a static method exposed through instance-like syntax; it does not change the receiver type. C# 14 adds extension blocks that can declare extension methods and properties. A conventional extension method remains useful for earlier compilers. Both examples use a top-level nongeneric static class.
using System;Console.WriteLine(8.IsEven());Console.WriteLine(8.IsEvenValue);static class NumberExtensions{ public static bool IsEven(this int value) => value % 2 == 0; extension(int value) { public bool IsEvenValue => value % 2 == 0; }}The traditional extension method and the C# 14 extension property compute the same predicate. Neither adds storage to int.
Expected output (reasoned, not executed):
TrueTrueField-backed properties
C# 14 allows field in a property accessor to refer to its compiler-synthesized backing field. You can validate writes without explicitly declaring a separate field. An ordinary explicit backing field works on earlier compilers. Keep the invariant and initialization behavior clear.
using System;var item = new StockItem();item.Quantity = 3;try { item.Quantity = -1; }catch (ArgumentOutOfRangeException) { Console.WriteLine("Rejected"); }Console.WriteLine(item.Quantity);class StockItem{ public int Quantity { get; set => field = value >= 0 ? value : throw new ArgumentOutOfRangeException(nameof(value)); }}The invalid assignment throws before the backing field changes. The default integer value of zero satisfies the invariant.
Expected output (reasoned, not executed):
Rejected3Common mistakes and edge cases
Use net10.0 and the matching C# 14 default or explicit LangVersion 14.0 for these examples, not latest or preview. A newer SDK alone does not change an existing target framework. Extension methods need scope/imports and do not override existing instance members. Other advanced constructs (indexers, operator overloads, partial declarations, namespace forms, preprocessor directives) remain separate coverage candidates; this lesson is a selective syntax/version comparison, not a complete language inventory.
Practice and recap
Write a C# 12 collection expression that appends 5 to [1,3]. Then rewrite the Quantity property using an explicit private field and explain which syntax requires C# 14.
Hint: Use an int[] target and a spread. Replace field with a named private member in both accessors.
Learning objective: Map a selected feature to its compiler and runtime needs. See the accompanying manual practice and knowledge check.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Modern syntax and language versions — knowledge check
Apply the lesson concepts to the following scenarios.
Why does var a = [1,2]; fail in C# 14?
What does ..classic do inside a collection expression?
Which feature was added in C# 14?
What does field mean in a C# 14 property accessor?
Which configuration matches the chosen teaching baseline?