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
[API Proposal]: Add Length and IsEmpty properties to System.BinaryData #77970
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/area-system-memory Issue DetailsBackground and motivationGetting the length of a There's also a trap where one could do Having a An API Proposalnamespace System;
public class BinaryData
{
public int Length { get; }
public bool IsEmpty { get; }
} API Usagevoid ProcessData(BinaryData data, string outputFile)
{
if (data.IsEmpty)
{
Console.WriteLine($"No data available to write to {outputFile}");
}
else
{
Console.WriteLine($"Writing {data.Length} bytes to {outputFile}");
// ... actual implementation ...
}
} Alternative DesignsNo response RisksThere are no risks associated with this proposition. The implementations of those properties are trivial and would just call respectively
|
Is there anything I should add to the proposal to initiate a discussion? Some months later I have worked with Ping @jeffhandley |
Apologies for the delay. The proposal looks good to me, I've marked it as ready for review. Looking at https://apireview.net/backlog I estimate that it should get reviewed within next two weeks. @0xced thank you! |
Looks good as proposed. We briefly discussed namespace System;
public class BinaryData
{
public int Length { get; }
public bool IsEmpty { get; }
} |
A pull request addressing this issue is ready: #86721 |
Background and motivation
Getting the length of a
System.BinaryData
instance is something quite natural. Currently, the best way to get the length is by doingdata.ToMemory().Length
which is not very pleasant.There's also a trap where one could do
data.ToArray().Length
or evendata.ToStream().Length
and this would allocate!Having a
Length
property solves both issues: code is more readable and ensures no allocation.An
IsEmpty
property would also be welcome.API Proposal
API Usage
Alternative Designs
No response
Risks
There are no risks associated with this proposition. The implementations of those properties are trivial and would just call respectively
_bytes.Length
and_bytes.IsEmpty
.The text was updated successfully, but these errors were encountered: