## Executive Summary

At .NET Conf 2026, Mano and Sergio Pedri officially unveiled **CsWinRT 3.0**, a ground-up re-architecture of the C#/WinRT projection toolchain that bridges modern .NET applications with native Windows Runtime (WinRT) APIs.

Originally introduced during the .NET 5 era, CsWinRT evolved across multiple .NET versions, retrofitting trimming and Native AOT support while preserving strict backwards compatibility. Over time, this backward-compatibility commitment created major developer friction: **version coupling and diamond dependency hell** across NuGet packages, **heavy Roslyn source generator overhead** that froze Visual Studio during editing, and **awkward code annotations** (`[DynamicWindowsRuntimeCast]`, mandatory `partial` classes, and array allocation workarounds).

CsWinRT 3.0 breaks through these architectural ceilings by introducing:
1. **Reference Projections Architecture:** Libraries now ship reference-only assemblies alongside raw WinMD metadata. The consumer application's chosen CsWinRT toolchain dynamically generates unified `WinRT.Projection.dll` and `WinRT.Interop.dll` assemblies at publish/build time, putting the app developer in 100% control of the projection version and eliminating package version mismatches.
2. **Massive Binary Size Savings on Native AOT:** 
   - **Microsoft Store Client:** Reduced executable footprint from **129 MB down to 107 MB** (**-22 MB, >16% reduction**).
   - **Contoso Camera App:** Down from **5.6 MB to 5.08 MB** (~10% reduction).
3. **IDE Inner-Loop Acceleration:** Removed expensive Roslyn source generators from the daily IntelliSense loop. Clean build generator execution dropped from **115 seconds to 9 seconds (13x faster)**; incremental builds dropped from **24 seconds to 4 seconds (6x faster)**.
4. **TFM Opt-in via the `.1` Suffix:** Non-breaking opt-in via Target Framework Monikers (e.g., `net11.0-windows10.0.26100.1` vs `.0`).
5. **Modern C# Array Projections via Spans:** WinRT Pass Arrays project as `ReadOnlySpan<T>`, Fill Arrays project as `Span<T>`, enabling zero-allocation slicing, stackalloc, and eliminating temporary array allocations.
6. **Native ABI Precision:** WinRT `Point`, `Rect`, and `Size` now use native `float` instead of legacy `double`, eliminating thousands of implicit conversions.

---

## 1. History & Motivation: Why Rebuild CsWinRT?

![Title: CsWinRT 3.0: Modernizing the Windows Projections for .NET](/dotnetconf2026/archive/27-cswinrt-3/screenshots/01_title_cswinrt3.webp)

When CsWinRT shipped in .NET 5, neither trimming nor Native AOT existed in modern .NET. As trimming arrived in .NET 6 and Native AOT in .NET 7/8, the team retrofitted compatibility incrementally. However, to avoid breaking existing binary NuGet packages, they could not alter generated ABI structures, assembly forwarding mechanisms, or type mappings.

![CsWinRT History and Motivation](/dotnetconf2026/archive/27-cswinrt-3/screenshots/02_cswinrt_history_and_motivation.webp)

This accumulated significant technical debt:
- Applications were forced into specific CsWinRT runtime versions by third-party UI controls.
- Developers had to sprinkle internal attributes (`[DynamicWindowsRuntimeCast]`) across everyday business logic.
- Roslyn analyzers evaluated massive projection metadata inside Visual Studio, causing noticeable typing stutter and delayed IntelliSense.

CsWinRT 3.0 resets this baseline by leveraging .NET 11 and C# 14 features natively.

---

## 2. Production Performance: Microsoft Store Client Benchmarks

![Binary Size: Microsoft Store Client Reduction](/dotnetconf2026/archive/27-cswinrt-3/screenshots/03_binary_size_store_reduction.webp)

Sergio shared real-world telemetry from the production **Microsoft Store Client** app running on Native AOT:

| Metric | CsWinRT 2.x (.NET 10 AOT) | CsWinRT 3.0 (.NET 10/11 AOT) | Impact / Speedup |
|---|---|---|---|
| **Microsoft Store Executable Size** | 129 MB | 107 MB | **-22 MB (-16.2% reduction)** |
| **Contoso Camera Executable Size** | 5.6 MB | 5.08 MB | **-0.52 MB (-9.3% reduction)** |
| **Store Clean Interop Gen CPU Time** | 115 seconds | 9 seconds | **12.8x faster** |
| **Store Incremental Build Time** | 24 seconds | 4 seconds | **6x faster** |

![Build Time and IntelliSense Wins](/dotnetconf2026/archive/27-cswinrt-3/screenshots/04_build_time_and_intellisense_wins.webp)

### Why Build Times Plummeted
In CsWinRT 2.x, a heavy Roslyn Source Generator scanned every syntax tree on every keystroke to determine marshalling requirements. In CsWinRT 3.0:
- The Roslyn source generator is completely removed from the IDE design-time loop.
- A standalone, native compiled interop generator runs exclusively when pressing F5 or publishing.
- Visual Studio remains responsive with instantaneous IntelliSense.

---

## 3. How WinRT & Projections Work

![What is WinRT & How Projections Work](/dotnetconf2026/archive/27-cswinrt-3/screenshots/05_what_is_winrt_projections.webp)

Windows Runtime (WinRT) APIs are described in language-agnostic `.winmd` (Windows Metadata) files. A language projection parses this metadata and produces types that feel native to that programming language:

```csharp
// Consuming WinRT via .NET TFM
// csproj: <TargetFramework>net11.0-windows10.0.26100.1</TargetFramework>

using Windows.Devices.Geolocation;

var locator = new Geolocator();
var pos = await locator.GetGeopositionAsync();
Console.WriteLine($"Latitude: {pos.Coordinate.Point.Position.Latitude}");
```

Developers can also author WinRT components in C# for consumption by C++, Rust, or JavaScript using `CsWinRTComponent=true`:

![Authoring WinRT Components in C#](/dotnetconf2026/archive/27-cswinrt-3/screenshots/06_authoring_winrt_components_csharp.webp)

![Idiomatic C# Surface: LINQ, Spans, and Pattern Matching](/dotnetconf2026/archive/27-cswinrt-3/screenshots/07_idiomatic_csharp_winrt_surface.webp)

Projections map WinRT types into native C# idioms:
- `Windows.Foundation.IAsyncOperation<T>` maps directly to `await Task<T>`.
- `IVectorView<T>` maps to `IReadOnlyList<T>`, allowing immediate use of LINQ (`Where`, `FirstOrDefault`).
- WinRT property bags accept modern C# **Collection Expressions** (`[ "Title", "Author" ]`).

---

## 4. Solving the Major Pain Points

### Pain Point 1: Version Coupling & Diamond Dependency Hell

![Pain Point: Version Coupling in CsWinRT 2.x](/dotnetconf2026/archive/27-cswinrt-3/screenshots/08_pain_point_version_coupling.webp)

In CsWinRT 2.x, NuGet component packages (e.g., UI controls, charting libraries) compiled and packaged concrete projection assemblies. If Package A was compiled with CsWinRT 2.0.8 and Package B with 2.1.0, the consuming application suffered assembly mismatch compilation errors. Even if a bug was fixed in CsWinRT 2.2, developers could not benefit until all upstream package authors recompiled and republished their packages.

### Pain Point 2: Trimming & AOT Boilerplate Friction

![Pain Point: AOT Friction with Attributes & Partial Classes](/dotnetconf2026/archive/27-cswinrt-3/screenshots/09_pain_point_aot_friction_attributes.webp)

To satisfy trimming and AOT compilers in 2.x, developers were forced to write awkward, unnatural C#:

```csharp
// CsWinRT 2.x: Unnatural boilerplate
public partial class TagButton : Button // Forced to be partial!
{
    [DynamicWindowsRuntimeCast(typeof(FrameworkElement))] // Required internal attribute!
    public void ProcessTags(object element)
    {
        if (element is FrameworkElement fe)
        {
            // Explicit array cast required to prevent compiler-generated hidden types
            TagHelper.ApplyTags((string[])[ "Tag1", "Tag2" ]);
        }
    }
}
```

In CsWinRT 3.0, all of this boilerplate is eliminated:

![CsWinRT 3.0: Clean, Natural C#](/dotnetconf2026/archive/27-cswinrt-3/screenshots/10_cswinrt3_clean_natural_csharp.webp)

```csharp
// CsWinRT 3.0: Clean, idiomatic C#
public class TagButton : Button // No partial required
{
    public void ProcessTags(object element)
    {
        if (element is FrameworkElement fe) // Clean pattern match
        {
            TagHelper.ApplyTags([ "Tag1", "Tag2" ]); // Pure collection expression
        }
    }
}
```

---

## 5. The Reference Projections Architecture

![Reference Projections Architecture](/dotnetconf2026/archive/27-cswinrt-3/screenshots/11_reference_projections_architecture.webp)

```mermaid
flowchart TD
    subgraph NuGetPackages["NuGet Packages / Component Libraries"]
        PkgA["Package A (ref/PackageA.dll + metadata/PackageA.winmd)"]
        PkgB["Package B (ref/PackageB.dll + metadata/PackageB.winmd)"]
        WinSDK["Windows SDK Targeting Pack (WinMDs)"]
    end

    subgraph AppBuild["Consumer Application Build Pipeline"]
        App["App Project (Chooses CsWinRT 3.0)"]
        Collector["WinMD Metadata Collector"]
        ProjGen["Projection Generator"]
        InteropGen["Native Compiled Interop Generator"]
    end

    subgraph EmittedAssemblies["Unified Emitted Assemblies"]
        WinRTProj["WinRT.Projection.dll (Unified Types)"]
        WinRTInterop["WinRT.Interop.dll (Analyzed App Interop)"]
    end

    PkgA --> Collector
    PkgB --> Collector
    WinSDK --> Collector
    Collector --> ProjGen
    App --> InteropGen
    ProjGen --> WinRTProj
    InteropGen --> WinRTInterop
```

### How Reference Projections Work:
1. **Reference-Only Packages:** Component packages only ship API signatures in `ref/` assemblies and raw metadata in a `metadata/` folder.
2. **App-Driven Compilation:** When the application builds, it gathers all referenced `.winmd` files.
3. **Unified Assemblies:** The app’s selected CsWinRT generator emits a single `WinRT.Projection.dll` (containing all projected types) and a single `WinRT.Interop.dll` (containing custom marshalling for classes implementing WinRT interfaces).
4. **Zero Diamond Dependencies:** Upstream package authors never lock consumers to a specific projection version.

---

## 6. The `.1` TFM Opt-In & Multi-Targeting

![TFM Opt-in via .1 Suffix and Multi-Targeting](/dotnetconf2026/archive/27-cswinrt-3/screenshots/12_tfm_dot_one_opt_in_system.webp)

To avoid breaking existing applications, Microsoft and the NuGet team introduced an explicit revision scheme in the Windows Target Framework Moniker (TFM):

| Target Framework Moniker | Projection Version | Compatibility Target |
|---|---|---|
| `net11.0-windows10.0.26100.0` | **CsWinRT 2.x** | Legacy applications, existing controls |
| `net11.0-windows10.0.26100.1` | **CsWinRT 3.0** | Modern reference projections, zero boilerplate |

For component authors who need to support both 2.x and 3.0 consumers, CsWinRT 3.0 provides the `#if CSWINRT3_0` preprocessor symbol:

```csharp
#if CSWINRT3_0
    // Uses .NET standard System.EventHandler<TTarget, TArgs>
    public event System.EventHandler<Timer, TickEventArgs> Tick;
#else
    // Legacy CsWinRT 2.x projection type
    public event Windows.Foundation.TypedEventHandler<Timer, TickEventArgs> Tick;
#endif
```

---

## 7. High-Performance Array Marshalling with `Span<T>`

![Span and ReadOnlySpan Array Marshalling](/dotnetconf2026/archive/27-cswinrt-3/screenshots/13_span_and_readonlyspan_marshalling.webp)

WinRT defines three array semantics:
1. **Pass Arrays (Input only):** Callee only reads from the array.
2. **Fill Arrays (Output buffer):** Caller passes an allocated buffer; callee populates it.
3. **Receive Arrays (Return array):** Callee allocates and returns an array.

Historically, all three mapped to `T[]`, causing severe allocation waste. CsWinRT 3.0 modernizes this mapping:

| WinRT Array Type | CsWinRT 2.x Mapping | CsWinRT 3.0 Mapping | Performance Advantage |
|---|---|---|---|
| **Pass Array** | `T[]` | `ReadOnlySpan<T>` | Supports stackalloc, span slicing, zero allocation |
| **Fill Array** | `T[]` | `Span<T>` | Direct slicing into existing memory buffers |
| **Receive Array** | `T[]` | `T[]` | Native array return retained |

![Code Comparison: Temporary Array Allocation vs Direct Span Slice](/dotnetconf2026/archive/27-cswinrt-3/screenshots/14_code_comparison_array_vs_span_slice.webp)

```csharp
// CsWinRT 2.x: Mandatory temporary allocation and copy
byte[] temp = new byte[16];
dataReader.ReadBytes(temp);
Array.Copy(temp, 0, destinationBuffer, offset, 16);

// CsWinRT 3.0: Zero-allocation direct span slice
dataReader.ReadBytes(destinationBuffer.AsSpan(offset, 16));
```

---

## 8. Live Migration Walkthrough (Contoso Camera App)

![Live Migration: Contoso Camera in Visual Studio](/dotnetconf2026/archive/27-cswinrt-3/screenshots/15_live_migration_contoso_camera.webp)

Mano walked through migrating the Contoso Camera sample app to CsWinRT 3.0:
1. **Update TFM:** Changed `net10.0-windows10.0.26100.0` to `net11.0-windows10.0.26100.1`.
2. **Package References:** Updated `Microsoft.Windows.CsWinRT` to `3.0.0-preview` and targeting pack to `88-preview`.
3. **Remove Legacy Interop Usings:** `using System.Runtime.InteropServices.WindowsRuntime;` is no longer required because extension methods (`AsTask`, `AsStream`) now live in `Windows.Foundation`.
4. **Strip AOT Attributes:** Deleted hundreds of `[DynamicWindowsRuntimeCast]` attributes across the codebase.
5. **Float Coordinates:** Updated Point/Rect coordinates to use native `float` values, matching native DirectX/WinUI math without hidden float-double conversions.

---

## Key Takeaways

| Feature | CsWinRT 2.x | CsWinRT 3.0 |
|---|---|---|
| **Projection Delivery** | Concrete assemblies in packages | Reference projections + on-the-fly merge |
| **Version Ownership** | Dictated by third-party packages | 100% owned by app developer |
| **Store App Size** | 129 MB | **107 MB (-16%)** |
| **IntelliSense Build** | 115s CPU time (Roslyn generator) | **9s CPU time (13x faster)** |
| **Array Marshalling** | `T[]` only | `ReadOnlySpan<T>` and `Span<T>` |
| **Syntax Overhead** | `partial`, custom cast attributes | Pure standard C# syntax |
| **Opt-in TFM** | `...26100.0` | **`...26100.1`** |
