From 1cee50a19de220ed16a03d9715adbb829f05e61a Mon Sep 17 00:00:00 2001 From: Matt Ellis Date: Thu, 18 Mar 2021 07:28:31 -0700 Subject: [PATCH] [System.Memory.Data] Add `Empty` to `BinaryData` (#49777) 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 --- .../System.Memory.Data/ref/System.Memory.Data.cs | 1 + .../System.Memory.Data/src/System/BinaryData.cs | 5 +++++ .../System.Memory.Data/tests/BinaryDataTests.cs | 13 +++++++++++++ 3 files changed, 19 insertions(+) diff --git a/src/libraries/System.Memory.Data/ref/System.Memory.Data.cs b/src/libraries/System.Memory.Data/ref/System.Memory.Data.cs index 4d0266a607cfd..d2bb7a7622d1d 100644 --- a/src/libraries/System.Memory.Data/ref/System.Memory.Data.cs +++ b/src/libraries/System.Memory.Data/ref/System.Memory.Data.cs @@ -12,6 +12,7 @@ public BinaryData(byte[] data) { } public BinaryData(object? jsonSerializable, System.Text.Json.JsonSerializerOptions? options = null, System.Type? type = null) { } public BinaryData(System.ReadOnlyMemory data) { } public BinaryData(string data) { } + public static BinaryData Empty { get; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] object? obj) { throw null; } public static System.BinaryData FromBytes(byte[] data) { throw null; } diff --git a/src/libraries/System.Memory.Data/src/System/BinaryData.cs b/src/libraries/System.Memory.Data/src/System/BinaryData.cs index 8e9744a74c881..35c1fcf9ae1cd 100644 --- a/src/libraries/System.Memory.Data/src/System/BinaryData.cs +++ b/src/libraries/System.Memory.Data/src/System/BinaryData.cs @@ -22,6 +22,11 @@ public class BinaryData /// private readonly ReadOnlyMemory _bytes; + /// + /// Returns an empty BinaryData. + /// + public static BinaryData Empty { get; } = new BinaryData(ReadOnlyMemory.Empty); + /// /// Creates a instance by wrapping the /// provided byte array. diff --git a/src/libraries/System.Memory.Data/tests/BinaryDataTests.cs b/src/libraries/System.Memory.Data/tests/BinaryDataTests.cs index b3ffa187f454b..9486ac8270f46 100644 --- a/src/libraries/System.Memory.Data/tests/BinaryDataTests.cs +++ b/src/libraries/System.Memory.Data/tests/BinaryDataTests.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; @@ -572,6 +573,18 @@ public void CloseStreamValidation() } + [Fact] + public void EmptyIsEmpty() + { + Assert.Equal(Array.Empty(), BinaryData.Empty.ToArray()); + } + + [Fact] + public void EmptyIsSingleton() + { + Assert.Same(BinaryData.Empty, BinaryData.Empty); + } + private class TestModel { public string A { get; set; }