-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Added [Memory|Span]Owner<T>.DangerousGetArray #3530
Added [Memory|Span]Owner<T>.DangerousGetArray #3530
Conversation
Thanks Sergio0694 for opening a Pull Request! The reviewers will test the PR and highlight if there is any conflict or changes required. If the PR is approved we will proceed to merge the pull request 🙌 |
Hello @michael-hawker! Because this pull request has the p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (
|
@msftbot auto merge if @vgromfeld or @azchohfi also approve. |
Hello @michael-hawker! Because you've given me some instructions on how to help merge this pull request, I'll be modifying my merge approach. Here's how I understand your requirements for merging this pull request:
If this doesn't seem right to you, you can tell me to cancel these instructions and use the auto-merge policy that has been configured for this repository. Try telling me "forget everything I just told you". |
PR Type
What kind of change does this PR introduce?
What is the current behavior?
There is currently no way to get the underlying
T[]
array from aMemoryOwner<T>
orSpanOwner<T>
instance without going through some hoops that are very inconvenient (and which are only possible forMemoryOwner<T>
). Being able to use the array directly is necessary when working with some older APIs that don't offer aSpan<T>
orMemory<T>
overload.What is the new behavior?
This PR introduces a new
DangerousGetArray
method that mirrors theMemoryMarshal.TryGetArray
method and works onMemoryOwner<T>
andSpanOwner<T>
instances. I've removed the try pattern since here the types guarantee that the underlying memory store will always be an array. The methods are calledDangerous___
because using the array is potentially dangerous in case a user keeps the array after disposing the original owner, as it means that that array might've been rented to some other consumer, so using it could lead to unexpected behavior. The methods are not inherently dangerous per se.API surface
Example usage
Suppose we have a
Person
class withstring Name
,string Surname
andint Age
properties, and we want to calculate an MD5 hash with the current state of the class. This was originally asked by a user in the C# Discord server (here).You can see how here we can leverage the new
DangerousGetArray
API to get the underlying array to use with theBitConverter.ToString
API, which doesn't have an overload accepting aReadOnlySpan<byte>
. The same goes for many other existing APIs that only accept an array as input data instead of the new memory APIs.PR Checklist
Please check if your PR fulfills the following requirements:
Pull Request has been submitted to the documentation repository instructions. Link:Sample in sample app has been added / updated (for bug fixes / features)Icon has been created (if new sample) following the Thumbnail Style Guide and templates