-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Add Empty
property to BinaryData
#49670
Comments
Tagging subscribers to this area: @roji, @ajcvickers Issue DetailsBackground and MotivationIt would be nice if there was a singleton Proposed APIPlease provide the specific public API signature diff that you are proposing. For example: namespace System
{
public class BinaryData {
+ public static BinaryData Empty { get; }
}
} Usage ExamplesBeforeprivate static readonly s_EmptyBinaryData = new BinaryData(Array.Empty<byte>());
public BinaryData SomeMethodThatReturnsData() {
if ( /* some check that sees if there's an data to return */) {
return s_EmptyBinaryData;
}
} (or, even worse, since we allocate each time!) public BinaryData SomeMethodThatReturnsData() {
if ( /* some check that sees if there's an data to return */) {
return new BinaryData(Array.Empty<byte>());
}
} Afterpublic BinaryData SomeMethodThatReturnsData() {
if ( /* some check that sees if there's an data to return */) {
return BinaryData.Empty;
}
} Alternative DesignsWe did not consider any alternate designs over a static get only property named RisksNone Identified.
|
Tagging subscribers to this area: @GrabYourPitchforks Issue DetailsBackground and MotivationIt would be nice if there was a singleton Proposed APIPlease provide the specific public API signature diff that you are proposing. For example: namespace System
{
public class BinaryData {
+ public static BinaryData Empty { get; }
}
} Usage ExamplesBeforeprivate static readonly s_EmptyBinaryData = new BinaryData(Array.Empty<byte>());
public BinaryData SomeMethodThatReturnsData() {
if ( /* some check that sees if there's an data to return */) {
return s_EmptyBinaryData;
}
} (or, even worse, since we allocate each time!) public BinaryData SomeMethodThatReturnsData() {
if ( /* some check that sees if there's an data to return */) {
return new BinaryData(Array.Empty<byte>());
}
} Afterpublic BinaryData SomeMethodThatReturnsData() {
if ( /* some check that sees if there's an data to return */) {
return BinaryData.Empty;
}
} Alternative DesignsWe did not consider any alternate designs over a static get only property named RisksNone Identified.
|
Fast-tracking this for review since it's from a partner team. |
It'd be nice to have an analyzer which detects |
We already have an analyzer for the first item (#43165 (comment)). Would probably be easy to spot the second as well. |
Also marked as blocking so it goes to the front of the queue |
May I suggest, if you change the type, to add a bit of documentation to: https://docs.microsoft.com/en-us/dotnet/api/system.binarydata?view=dotnet-plat-ext-5.0 or link https://docs.microsoft.com/en-us/dotnet/api/overview/azure/system.memory.data-readme? I actually googled for BinaryData, as I was not aware of the type and the second link is hard to find (it shows up for system.memory.data, but not for binarydata), the second link is part of azure for developers for some reason. |
namespace System
{
public class BinaryData
{
public static BinaryData Empty { get; }
}
} |
@terrajobst I assume it would be helpful to the team if I dusted off my fork and submitted a PR, since this is now approved? |
@ellismg have at it! |
This is useful for APIs which return `BinaryData` and want to use a singleton instead of allocating a new `BinaryData` each time. Previously, developers would need to create their own static copy of an empty binary data, now they can just use `Empty` Fixes dotnet#49670
This is useful for APIs which return `BinaryData` and want to use a singleton instead of allocating a new `BinaryData` each time. Previously, developers would need to create their own static copy of an empty binary data, now they can just use `Empty` Fixes #49670
Background and Motivation
It would be nice if there was a singleton
Empty
property forBinaryData
, which wraps an empty array. Without this, everyone who wants to returnBinaryData
needs to create the own static copy of an empty binary data, if they want to share this instance instead of allocating a new empty binary data each time.Proposed API
Please provide the specific public API signature diff that you are proposing. For example:
namespace System { public class BinaryData { + public static BinaryData Empty { get; } } }
Usage Examples
Before
(or, even worse, since we allocate each time!)
After
Alternative Designs
We did not consider any alternate designs over a static get only property named
Empty
.Risks
None Identified.
The text was updated successfully, but these errors were encountered: