From 7402ef0005412d210e44eaf4784d0eb784cac16c Mon Sep 17 00:00:00 2001 From: Gidado Mukhtar Balangoggo Date: Sat, 29 Aug 2026 00:22:07 +0100 Subject: [PATCH 1/3] Add common patterns for field keyword in C# 14 Added practical examples showing three common patterns when using field-backed properties: Lazy initialization MVVM property notifications Data normalization These patterns help developers understand real-world usage beyond the null-checking example. --- docs/csharp/whats-new/csharp-14.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/docs/csharp/whats-new/csharp-14.md b/docs/csharp/whats-new/csharp-14.md index 5a4261fd6edfa..c898198b5594e 100644 --- a/docs/csharp/whats-new/csharp-14.md +++ b/docs/csharp/whats-new/csharp-14.md @@ -90,6 +90,30 @@ public string Message get; set => field = value ?? throw new ArgumentNullException(nameof(value)); } +### Common Patterns with `field` + +**Lazy Initialization:** +```csharp +public SqlConnection Connection +{ + get => field ??= new SqlConnection("connection-string"); +} +public string DisplayName +{ + get; + set + { + if (field == value) return; + field = value; + OnPropertyChanged(); + } +} +public string Email +{ + get; + set => field = value?.Trim().ToLower() ?? ""; +} + ``` You can declare a body for one or both accessors for a field backed property. From 2cfb00bf66bee9acc8fa1ee9e850754200eb3621 Mon Sep 17 00:00:00 2001 From: Gidado Mukhtar Balangoggo Date: Sat, 29 Aug 2026 00:47:24 +0100 Subject: [PATCH 2/3] fix: correct markdown formatting in field keyword patterns section Fixed code fence formatting in the "Common patterns with field" section. Changes: - Removed duplicate opening code fence that was preventing proper markdown rendering - Ensured code blocks display correctly for all three patterns (Lazy Initialization, MVVM, Data Normalization) - Changed heading to lowercase for consistency with existing documentation style The code examples now render correctly in the documentation. --- docs/csharp/whats-new/csharp-14.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/csharp/whats-new/csharp-14.md b/docs/csharp/whats-new/csharp-14.md index c898198b5594e..ca092b107cd31 100644 --- a/docs/csharp/whats-new/csharp-14.md +++ b/docs/csharp/whats-new/csharp-14.md @@ -90,7 +90,8 @@ public string Message get; set => field = value ?? throw new ArgumentNullException(nameof(value)); } -### Common Patterns with `field` +``` +### Common patterns with `field` **Lazy Initialization:** ```csharp From e9607eb8426a5dde0f4e2e10d29e820a0669a11a Mon Sep 17 00:00:00 2001 From: Gidado Mukhtar Balangoggo Date: Sat, 29 Aug 2026 00:53:38 +0100 Subject: [PATCH 3/3] fix: use null-safe chaining in Email normalization example Fixed potential null reference exception in the Email property example. Changes: - Added null-safe operator before ToLowerInvariant() to prevent NullReferenceException - Changed ToLower() to ToLowerInvariant() for culture-invariant normalization - Used string.Empty instead of "" for consistency with documentation standards The original code would throw an exception if value was null because it attempted to call .ToLower() on the result of value?.Trim(), which could be null. The fix ensures proper null handling throughout the chain. --- docs/csharp/whats-new/csharp-14.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/whats-new/csharp-14.md b/docs/csharp/whats-new/csharp-14.md index ca092b107cd31..976e56b70ab80 100644 --- a/docs/csharp/whats-new/csharp-14.md +++ b/docs/csharp/whats-new/csharp-14.md @@ -112,7 +112,7 @@ public string DisplayName public string Email { get; - set => field = value?.Trim().ToLower() ?? ""; + set => field = value?.Trim()?.ToLowerInvariant() ?? string.Empty; } ```