Add common patterns for field keyword in C# 14 - #55777
Open
gidadomukhtar wants to merge 3 commits into
Open
Conversation
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.
Contributor
There was a problem hiding this comment.
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 Thefieldkeyword. - 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.
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
commented
Aug 28, 2026
gidadomukhtar
left a comment
Author
There was a problem hiding this comment.
Fixed the null-safe chaining issue in the Email example:
- Changed to use
?.ToLowerInvariant()instead of.ToLower()to handle null properly - Used
string.Emptyfor consistency
The updated code now safely handles null values without throwing exceptions.
Author
@dotnet-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Build report