Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 22, 2025

This PR adds documentation for a potential workaround to compiler error CS0677 by using nint as an alternative to long when declaring volatile fields.

The CS0677 error occurs when attempting to use the volatile keyword with types that cannot guarantee atomic access, such as long:

class TestClass
{
   private volatile long i;   // CS0677: a volatile field cannot be of the type 'long'
}

The documentation now includes a "Potential workarounds" section that explains how nint (native-sized integer) can be used as an alternative:

class TestClass
{
   private volatile nint i;   // This compiles successfully
}

The workaround section explains:

  • How nint guarantees atomic access and works with volatile
  • Platform considerations (32-bit vs 64-bit behavior)
  • When this approach is most appropriate
  • Cross-references to related documentation about native-sized integers
  • General guidance on thread-safe programming alternatives

This addresses a gap in the documentation where developers encountering CS0677 with long had no guidance on potential solutions.

Fixes #45367.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


Internal previews

📄 File 🔗 Preview link
docs/csharp/misc/cs0677.md docs/csharp/misc/cs0677

Copilot AI and others added 2 commits August 22, 2025 18:12
Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Missing a way to work around CS0677 Add CS0677 workaround documentation using nint Aug 22, 2025
@Copilot Copilot AI requested a review from BillWagner August 22, 2025 18:17
Copilot finished work on behalf of BillWagner August 22, 2025 18:17
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

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

This is ready for final review, with my suggested changed.

@BillWagner BillWagner marked this pull request as ready for review August 25, 2025 15:59
@BillWagner BillWagner requested a review from a team as a code owner August 25, 2025 15:59
@BillWagner BillWagner requested a review from adegeo August 25, 2025 15:59
BillWagner and others added 2 commits August 25, 2025 14:49
Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
@Copilot Copilot AI requested a review from BillWagner August 25, 2025 18:58
Copilot finished work on behalf of BillWagner August 25, 2025 18:58
Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Copilot finished work on behalf of BillWagner August 25, 2025 19:05
@BillWagner BillWagner merged commit b417a57 into main Aug 25, 2025
10 checks passed
@BillWagner BillWagner deleted the copilot/fix-45367 branch August 25, 2025 19:16
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.

Missing a way to work around CS0677
3 participants