Skip to content

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

Merged
merged 1 commit into from
Jul 20, 2025
Merged

Conversation

stephentoub
Copy link
Member

No description provided.

@Copilot Copilot AI review requested due to automatic review settings July 18, 2025 15:31
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jul 18, 2025
Copy link
Contributor

@Copilot Copilot AI left a 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

@stephentoub stephentoub added area-Meta and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jul 18, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-meta
See info in area-owners.md if you want to be subscribed.

@stephentoub stephentoub enabled auto-merge (squash) July 20, 2025 11:46
@stephentoub stephentoub merged commit a656a36 into dotnet:main Jul 20, 2025
126 of 151 checks passed
xtqqczze added a commit to xtqqczze/dotnet-runtime that referenced this pull request Jul 21, 2025
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
@stephentoub stephentoub deleted the morefield branch July 21, 2025 02:59
@ladeak
Copy link
Contributor

ladeak commented Jul 21, 2025

Is there a code analyzer to identify the refactoring opportunity, or is it done one-by-one?

@stephentoub
Copy link
Member Author

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.

stephentoub pushed a commit that referenced this pull request Jul 25, 2025
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants