Explore primary constructors, collection expressions, spread element, inline arrays, and interceptors in C# 12.
0 / 5 completed
1 / 5
What are primary constructors in C# 12?
Primary constructors:class Point(double x, double y) { public double Distance => Math.Sqrt(x*x + y*y); }. The parameters x and y are captured in the class body. Previously available only on records, C# 12 extends this to all classes and structs, reducing boilerplate field declarations.
2 / 5
What do collection expressions in C# 12 allow?
Collection expressions:int[] arr = [1, 2, 3]; and List<int> list = [1, 2, 3]; use the same syntax. The spread operator [..a, ..b] concatenates collections. The compiler chooses the most efficient construction strategy for each target type, often avoiding intermediate allocations.
3 / 5
What are inline arrays in C# 12?
Inline arrays: used by the runtime team to implement Span<T> and other performance primitives. A [InlineArray(8)] struct with one field stores 8 elements contiguously without heap allocation. Normal index syntax works via compiler magic, and they support Span<T> slicing.
4 / 5
What is the spread element .. in C# 12 collection expressions?
Spread element:string[] all = [..greetings, ..farewells]; concatenates two arrays. The compiler generates efficient concatenation code. Combined with collection expressions, it replaces verbose Concat LINQ calls and array copying while remaining readable.
5 / 5
What are interceptors in C# 12 (preview feature)?
Interceptors: a source generator can emit [InterceptsLocation("File.cs", line, col)] on a method, redirecting all calls at that source location to the interceptor at compile time. Used by ASP.NET Core Minimal APIs to replace reflection-heavy code with generated code for improved AOT compatibility.