Clarify sizeof rules for unsafe code blocks#51543
Open
BillWagner wants to merge 3 commits intodotnet:mainfrom
Open
Clarify sizeof rules for unsafe code blocks#51543BillWagner wants to merge 3 commits intodotnet:mainfrom
sizeof rules for unsafe code blocks#51543BillWagner wants to merge 3 commits intodotnet:mainfrom
Conversation
Some rules regarding nint, nuint, and ref struct types wasn't as clear as it should be. Fixes dotnet#51281
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request clarifies the documentation for the sizeof operator, specifically addressing issue #51281 regarding the rules for using sizeof with different types in safe and unsafe code contexts. The changes improve accuracy by explicitly stating that nint, nuint, and ref struct types like Span<T> require unsafe context, and provide concrete examples demonstrating their usage.
Changes:
- Updated documentation to clarify that safe code requires built-in unmanaged types whose size is not platform-dependent
- Rewrote the unsafe code section with more precise descriptions of how
sizeofworks with different type categories - Added code examples demonstrating
sizeofwithnint,nuint, andSpan<int>in unsafe blocks
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| docs/csharp/language-reference/operators/sizeof.md | Updated documentation to clarify platform-dependent type restrictions and unsafe code rules, with improved descriptions of reference, value, and ref struct type behavior |
| docs/csharp/language-reference/operators/snippets/shared/SizeOfOperator.cs | Added three new examples within the unsafe block demonstrating sizeof usage with nint, nuint, and Span<int> |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Some rules regarding nint, nuint, and ref struct types wasn't as clear as it should be. Add examples for
nint,nuint, andSpan<T>.Fixes #51281
Internal previews