Skip to content

[Version 11.0] Feature support for Numeric IntPtr#1598

Draft
RexJaeschke wants to merge 4 commits intodraft-v11from
v11-numeric-IntPtr
Draft

[Version 11.0] Feature support for Numeric IntPtr#1598
RexJaeschke wants to merge 4 commits intodraft-v11from
v11-numeric-IntPtr

Conversation

@RexJaeschke
Copy link
Contributor

@RexJaeschke RexJaeschke commented Mar 7, 2026

This is Rex's adaptation of the corresponding MS proposal.

Issue 1

Since .NET 5, IntPtr and UIntrPtr have had properties MinValue and MaxValue. Shall we admit that and add these members to the library annex as new members of an existing type?

Issue 2

Should we add any new operators (such as addition and subtraction) to IntPtr/UIntPtr in the library annex?

Issue 3

The following text was added to 8.3.5 Simple types in Rex's Draft V9 PR #1457. Please review it to see which text should be kept, which should be deleted, and which should be changed.

Although nint and nuint shall be represented by the types System.IntPtr and System.UIntPtr, respectively, nint and nuint are not aliases for those types. As such, not all members of the corresponding System types are defined for nint and nuint. Instead, the compiler shall make available additional conversions and operations for the types System.IntPtr and System.UIntPtr, when used in the context of native integer types.
Consider the following:

nint a1 = 1;           // OK
System.IntPtr a2 = 1;  // Error: no implicit conversion

While the implementation provides operations and conversions for nint and nuint that are appropriate for integer types, those operations and conversions are not available on the System type counterparts. Similarly,

M((nint)1);

static void M(dynamic d)
{
    var v = d >> 2; // RuntimeBinderException: '>>' cannot be applied to operands
                    // of type System.IntPtr/System.UIntPtr and int
}

The only constructor for nint or nuint is the parameter-less constructor.

The following members of System.IntPtr and System.UIntPtr are explicitly excluded from nint or nuint:

// constructors
// arithmetic operators
// implicit and explicit conversions
public static readonly IntPtr Zero; // use 0 instead
public static int Size { get; }     // use sizeof() instead
public static IntPtr Add(IntPtr pointer, int offset);
public static IntPtr Subtract(IntPtr pointer, int offset);
public int ToInt32();
public long ToInt64();
public void* ToPointer();

The remaining members of System.IntPtr and System.UIntPtr are implicitly included in nint and nuint. These are:

public override bool Equals(object obj);
public override int GetHashCode();
public override string ToString();
public string ToString(string format);

Interfaces implemented by System.IntPtr and System.UIntPtr are implicitly included in nint and nuint, with occurrences of the underlying types replaced by the corresponding native integer types. For example, if IntPtr implements ISerializable, IEquatable<IntPtr>, and IComparable<IntPtr>, then nint implements ISerializable, IEquatable<nint>, and IComparable<nint>.

nint and System.IntPtr, and nuint and System.UIntPtr, are considered equivalent for overriding, hiding, and implementing, however.

Overloads cannot differ by nint and System.IntPtr, and nuint and System.UIntPtr, alone. However, overrides and implementations may differ by nint and System.IntPtr, or nuint and System.UIntPtr, alone.

Methods hide other methods that differ by nint and System.IntPtr, or nuint and System.UIntPtr, alone.

typeof(nint) is typeof(System.IntPtr), and typeof(nuint) is typeof(System.UIntPtr).[

@RexJaeschke RexJaeschke added this to the C# 11 milestone Mar 7, 2026
@RexJaeschke RexJaeschke added type: feature This issue describes a new feature Review: pending Proposal is available for review labels Mar 7, 2026
@RexJaeschke RexJaeschke marked this pull request as draft March 7, 2026 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review: pending Proposal is available for review type: feature This issue describes a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant