Skip to content

Add common patterns for field keyword in C# 14 - #55777

Open
gidadomukhtar wants to merge 3 commits into
dotnet:mainfrom
gidadomukhtar:patch-1
Open

Add common patterns for field keyword in C# 14#55777
gidadomukhtar wants to merge 3 commits into
dotnet:mainfrom
gidadomukhtar:patch-1

Conversation

@gidadomukhtar

@gidadomukhtar gidadomukhtar commented Aug 28, 2026

Copy link
Copy Markdown

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.

Summary

Describe your changes here.

Fixes #Issue_Number (if available)


Internal previews

File Preview link
docs/csharp/whats-new/csharp-14.md Learn preview

Build report

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.
Copilot AI lite review requested due to automatic review settings August 28, 2026 23:23
@gidadomukhtar
gidadomukhtar requested review from a team and BillWagner as code owners August 28, 2026 23:23
@dotnetrepoman dotnetrepoman Bot added this to the August 2026 milestone Aug 28, 2026
@dotnet-policy-service dotnet-policy-service Bot added dotnet-csharp/svc whats-new/subsvc community-contribution Indicates PR is created by someone from the .NET community. labels Aug 28, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request expands the C# 14 “field backed properties” section with additional practical examples intended to illustrate common real-world patterns.

Changes:

  • Adds a new “Common patterns with field” section under The field keyword.
  • Introduces example code covering lazy initialization, MVVM-style property change notifications, and data normalization.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread docs/csharp/whats-new/csharp-14.md
Comment thread docs/csharp/whats-new/csharp-14.md Outdated
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.
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.

@gidadomukhtar gidadomukhtar left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the null-safe chaining issue in the Email example:

  • Changed to use ?.ToLowerInvariant() instead of .ToLower() to handle null properly
  • Used string.Empty for consistency

The updated code now safely handles null values without throwing exceptions.

@gidadomukhtar

Copy link
Copy Markdown
Author

@gidadomukhtar please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@dotnet-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@dotnet-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@dotnet-policy-service agree company="Microsoft"

Contributor License Agreement

@dotnet-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Indicates PR is created by someone from the .NET community. dotnet-csharp/svc whats-new/subsvc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants