Skip to content

Add Empty property to BinaryData #49670

Description

@ellismg

Background and Motivation

It would be nice if there was a singleton Empty property for BinaryData, which wraps an empty array. Without this, everyone who wants to return BinaryData 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

private static readonly s_EmptyBinaryData = new BinaryData(Array.Empty<byte>());

public BinaryData SomeMethodThatReturnsData() {
    if ( /* some check that sees if there's any 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 any data to return */) {
        return new BinaryData(Array.Empty<byte>());
    }
}

After

public BinaryData SomeMethodThatReturnsData() {
    if ( /* some check that sees if there's any data to return */) {
        return BinaryData.Empty;
    }
}

Alternative Designs

We did not consider any alternate designs over a static get only property named Empty.

Risks

None Identified.

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Memorycode-analyzerMarks an issue that suggests a Roslyn analyzercode-fixerMarks an issue that suggests a Roslyn code fixer

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions