docs
docs copied to clipboard
`nint` and `nuint` types Constants specifies range for 32-bit process
Section Constants says:
Constants
You can use constant values in the following ranges:
- For
nint: Int32.MinValue to Int32.MaxValue.- For
nuint: UInt32.MinValue to UInt32.MaxValue.
This is only correct for 32-bit processes.
As nint and nuint are native sized, so on 64-bit processes the range for nint is Int64.MinValue to Int64.MaxValue.
Or generalized: the range is the same as for IntPtr, so [IntPtr.MinSize, IntPtr.MaxSize]
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 71aa383e-70a4-855c-e639-c06c479f918e
- Version Independent ID: 7c19d234-b117-196d-d1a9-d29be9de5ff2
- Content: nint and nuint types - C# reference
- Content Source: docs/csharp/language-reference/builtin-types/nint-nuint.md
- Product: dotnet-csharp
- Technology: csharp-language-reference
- GitHub Login: @BillWagner
- Microsoft Alias: wiwagn
I think the docs are correct. It talks specifically about "constants". In general, the range of nint and nuint will be dependent on whether you're on 32-bit or 64-bit, but a constant nint or a constant nuint, you're limited by the more restrictive range. That's a reason why there is no nint.MaxValue.
But there's IntPtr.MaxValue and IntPtr.MinValue -- as properties and not const, because of the "evaluation at runtime" as JIT constant.
The docs are incorrect, as they allude that only [int.MinValue, int.MaxValue] is allowed, which in turn defeat a motivation behind of nint.
Thanks for noting this @gfoidl
I also think the docs are correct. I'm hoping to point out the distinction between those values that can be compile-time constants, and those that are evaluated at runtime.
Adding @tannergooding for any suggestions to improve the clarity here.
Yeah, the docs are correct. Sorry for the statement above that they're incorrect. I got trapped here.
What about:
Constants
You can use constant values in the following ranges:
- For
nint: Int32.MinValue to Int32.MaxValue.- For
nuint: UInt32.MinValue to UInt32.MaxValue.Note: at runtime you can assign values in the range
[nint.MinValue, nint.MaxValue], but for constant values at compile-time the smallest common range must be used to avoid potential overflow.
? (a bit better wording, then it should be clear).
The docs are correct. We might be able to add a statement differentiating between compile time constant vs supported range.
Maybe something like:
While the full range of
nintandnuintmay be larger, compile time constants are restricted to a 32-bits range:
- For nint: Int32.MinValue to Int32.MaxValue.
- For nuint: UInt32.MinValue to UInt32.MaxValue.
or something like what @gfoidl suggested.
Similarly, System.Int64 also has a reference to 32-bit that looks like it got overlooked.