-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Use C# 14's field keyword in more places #117817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adopts C# 14's field
keyword to replace explicit backing fields in lazy initialization patterns across the codebase. The field
keyword enables properties to access their compiler-generated backing field directly, eliminating the need for explicitly declared private fields used solely for lazy initialization.
- Converts lazy property initialization patterns to use the
field
keyword - Removes explicit private backing fields that are no longer needed
- Maintains identical functionality while reducing code verbosity
Reviewed Changes
Copilot reviewed 143 out of 143 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
System.Windows.Extensions/SystemSounds.cs | Converts static property lazy initialization from explicit fields to field keyword |
System.Transactions.Local/TransactionManager.cs | Replaces private backing fields with field for configuration settings |
System.Text.RegularExpressions (multiple files) | Updates regex engine components to use field for cached objects |
System.Security.Cryptography.Xml (multiple files) | Converts XML crypto classes to use field for lazy properties |
System.Runtime.Serialization (multiple files) | Updates serialization infrastructure to use field keyword |
System.Reflection.MetadataLoadContext (multiple files) | Converts reflection types to use field for name and metadata caching |
System.Private.Xml (multiple files) | Updates XML processing classes to use field for lazy initialization |
System.Private.DataContractSerialization (multiple files) | Converts data contract serialization to use field keyword |
System.Private.CoreLib (multiple files) | Updates core library types to use field for static and instance properties |
System.Net.* (multiple files) | Converts networking libraries to use field for lazy initialization |
System.ComponentModel.* (multiple files) | Updates component model types to use field keyword |
System.Collections.* (multiple files) | Converts collection types to use field for lazy properties |
System.CodeDom (multiple files) | Updates CodeDOM classes to use field for collection properties |
Tagging subscribers to this area: @dotnet/area-meta |
Replaced `Volatile.Read` with plain field reads. The memory barriers provided by `CompareExchange` are sufficient to ensure thread safety and visibility, making `Volatile.Read` unnecessary. Follow-up to dotnet#117817 Related: dotnet#100969 cc: @stephentoub
Is there a code analyzer to identify the refactoring opportunity, or is it done one-by-one? |
There is one in VS. This was a combination. |
Replaced `Volatile.Read` with plain field reads. The memory barriers provided by `CompareExchange` are sufficient to ensure thread safety and visibility, making `Volatile.Read` unnecessary. Follow-up to #117817 Related: #100969 cc: @stephentoub Co-authored-by: Tanner Gooding <[email protected]>
No description provided.