Skip to content

[API Proposal]: System.Collection.BitArray - New constructors to pass a ReadOnlySpan<T> where T is int, bool or byte #80263

Description

@jimmygilles

Background and motivation

Hello,

The System.Collection.BitArray has a constructor that accepts an array of boolean, byte or int.
As we now have ReadOnlySpan<byte>, ReadOnlySpan<bool> and ReadOnlySpan<int>, that would be great to add three new constructors to accept these span.

Currently we must do a ".ToArray" on the span.

API Proposal

namespace System.Collections;

public sealed class BitArray : ICollection, ICloneable    // So, the existing one
{
    public BitArray(ReadOnlySpan<byte> bytes);
    public BitArray(ReadOnlySpan<bool> values);
    public BitArray(ReadOnlySpan<int> values);
}

API Usage

ReadOnlySpan<int> myIntSpan = new[] { 1, 2, 3 }
var array1 = new BitArray(myIntSpan);

ReadOnlySpan<bool> myBoolSpan = new[] { true, false, true }
var array2 = new BitArray(myBoolSpan);

ReadOnlySpan<byte> myByteSpan = new[] { 5, 6, 7 }
var array3 = new BitArray(myByteSpan);

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Collectionsin-prThere is an active PR which will close this issue when it is mergedwishlistIssue we would like to prioritize, but we can't commit we will get to it yet

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions