docs icon indicating copy to clipboard operation
docs copied to clipboard

`nint` and `nuint` types Constants specifies range for 32-bit process

Open gfoidl opened this issue 4 years ago • 6 comments

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.

gfoidl avatar May 19 '21 10:05 gfoidl

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.

Youssef1313 avatar May 19 '21 11:05 Youssef1313

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.

gfoidl avatar May 19 '21 12:05 gfoidl

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.

BillWagner avatar May 19 '21 13:05 BillWagner

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).

gfoidl avatar May 19 '21 13:05 gfoidl

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 nint and nuint may 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.

tannergooding avatar May 19 '21 15:05 tannergooding

Similarly, System.Int64 also has a reference to 32-bit that looks like it got overlooked.

dodexahedron avatar Apr 18 '24 02:04 dodexahedron