-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Use nint
for native-sized integers in Marshal
#118335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
xtqqczze
commented
Aug 4, 2025
- Fix CA2020: Prevent behavioral change
* Fix CA2020: Prevent behavioral change
Tagging subscribers to this area: @dotnet/interop-contrib |
There was a problem hiding this 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?
Ref assembly sources are auto-formatted. I would do any reformatting of ref assemblies in dedicated PRs. |
Related: dotnet/dotnet-api-docs#11652 |
I’m out of the loop a little here: why is this change necessary? Just noting that this looks strange:
|
It is no more strange than APIs use the framework type name such as There is no semantic difference (on modern .NET) between |
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 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. |