Skip to content

Conversation

xtqqczze
Copy link
Contributor

@xtqqczze xtqqczze commented Aug 4, 2025

  • Fix CA2020: Prevent behavioral change

* Fix CA2020: Prevent behavioral change
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Aug 4, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

Copy link
Member

@AaronRobinsonMSFT AaronRobinsonMSFT left a comment

Choose a reason for hiding this comment

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

Should we also update the ref assembly definitions?

@jkotas
Copy link
Member

jkotas commented Aug 4, 2025

Should we also update the ref assembly definitions?

Ref assembly sources are auto-formatted. I would do any reformatting of ref assemblies in dedicated PRs.

@jkotas jkotas merged commit cb44822 into dotnet:main Aug 4, 2025
145 checks passed
@xtqqczze xtqqczze deleted the nint-Marshal branch August 4, 2025 18:05
xtqqczze added a commit to xtqqczze/dotnet-api-docs that referenced this pull request Aug 5, 2025
@xtqqczze
Copy link
Contributor Author

xtqqczze commented Aug 5, 2025

Related: dotnet/dotnet-api-docs#11652

AaronRobinsonMSFT pushed a commit to dotnet/dotnet-api-docs that referenced this pull request Aug 5, 2025
@agocke
Copy link
Member

agocke commented Aug 5, 2025

I’m out of the loop a little here: why is this change necessary? Just noting that this looks strange:

public static nint ReadIntPtr(object ptr, int ofs)

@tannergooding
Copy link
Member

tannergooding commented Aug 5, 2025

It is no more strange than static int ReadInt32(object ptr, int ofs)

APIs use the framework type name such as Int32 or IntPtr. Code style is then to prefer the language keyword such as int or nint.

There is no semantic difference (on modern .NET) between IntPtr vs nint and the IDE, IntelliSense, docs, and most other places default to using the language keyword. There is only the historical difference in that some people used IntPtr to represent handles or pointers, rather than using a more correct type (like safe handle or void*).

@jkotas
Copy link
Member

jkotas commented Aug 5, 2025

It is both about code style and clarify about unsafe code boundaries.

Historically, IntPtr has been used to represent both integers and unmanaged pointers. We want to change the places where it is used as an integer to nint, and the places where it is as used as a pointer to use unmanaged pointer. The later one is much more common under libraries in this repo. We have approved API overloads to help with this transition: #75630.

There are situations where the meaning is ambiguous. It can be either number or pointer. I think it makes sense to go with nint in ambiguous cases like ReadIntPtr.

And there are situations where IntPtr represents pointer, but we cannot change it to be actual pointer since it is part of public API. We can keep them as IntPtr in the source to make it clear that it is legacy baggage, or we can change them to nint for more consistent (but also somewhat more confusing) style. I do not have a strong opinion. It is relatively few places in the grand scheme of things.

@github-actions github-actions bot locked and limited conversation to collaborators Sep 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime.InteropServices community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants