Skip to content

Commit b417a57

Browse files
CopilotBillWagnergewarren
authored
Add CS0677 workaround documentation using nint (#48072)
* Initial plan * Initial exploration and plan setup Co-authored-by: BillWagner <[email protected]> * Add CS0677 workaround documentation using nint Co-authored-by: BillWagner <[email protected]> * Update .gitignore * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> * Fix bullet point formatting and directional language in CS0677 docs Co-authored-by: BillWagner <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: Bill Wagner <[email protected]> Co-authored-by: Genevieve Warren <[email protected]>
1 parent 2e35e21 commit b417a57

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

docs/csharp/misc/cs0677.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ ms.assetid: 6a4a3703-9b44-4c4f-a564-8b437b1cb6b8
1414

1515
Fields declared with the `volatile` keyword must be one of the following types:
1616

17-
- Any reference type
17+
- Any reference type.
1818

19-
- Any pointer type (in an `unsafe` context)
19+
- Any pointer type (in an `unsafe` context).
2020

21-
- The types `sbyte`, **byte**, **short**, `ushort`, `int`, `uint`, `char`, **float**, `bool`
21+
- The types `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `char`, `float`, `bool`.
2222

23-
- Enum types based on any of the above types
23+
- Enum types based on any of the listed types.
2424

2525
The following sample generates CS0677:
2626

@@ -35,3 +35,28 @@ class TestClass
3535
}
3636
}
3737
```
38+
39+
## Potential workarounds
40+
41+
In some scenarios, you might be able to use `nint` (native-sized integer) instead of `long` as a workaround for CS0677. The `nint` type is guaranteed to support atomic access and can be used with the `volatile` keyword:
42+
43+
```csharp
44+
class TestClass
45+
{
46+
private volatile nint i; // This compiles successfully
47+
48+
public static void Main()
49+
{
50+
}
51+
}
52+
```
53+
54+
The `nint` type is a native-sized integer that's 32-bit on 32-bit platforms and 64-bit on 64-bit platforms. On 64-bit platforms, `nint` has the same size and range as `long`, but it's designed to guarantee atomic access. This workaround is most appropriate when:
55+
56+
- Your code targets 64-bit platforms where `nint` provides the same range as `long`.
57+
- You need atomic access to a large integer value in a multithreaded context.
58+
- Platform-specific integer size behavior is acceptable for your use case.
59+
60+
For more information about native-sized integers, see [Integral numeric types](../language-reference/builtin-types/integral-numeric-types.md#native-sized-integers).
61+
62+
As general guidance on thread-safe programming, consider using <xref:System.Threading.Interlocked> operations or the [`lock`](../language-reference/statements/lock.md) statement instead of `volatile` fields.

0 commit comments

Comments
 (0)