Skip to content

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

Closed

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

Activity

  1. added
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Jan 5, 2023
  2. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jan 5, 2023
  3. DrkWzrd commented on Jan 5, 2023

    @DrkWzrd

    array1 should have 96 bits; array2 3 bits and array3 (there's a typo in the example) 24 bits, isn't it?

  4. jimmygilles commented on Jan 5, 2023

    @jimmygilles
    Author

    (Sorry for the typo.)
    Yes, it should be 96, 3 and 24.
    In fact it must follow the same behaviour as the existing ones with array (int[], bool[], byte[]).

    My request here is just to avoid an unnecessary "ToArray()" on the span.

  5. removed
    untriagedNew issue has not been triaged by the area owner
    on Jan 9, 2023
  6. added this to the Future milestone on Jan 9, 2023
  7. added
    wishlistIssue we would like to prioritize, but we can't commit we will get to it yet
    on Jan 10, 2023
  8. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 17, 2023
  9. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 17, 2023
  10. thomasnalix commented on Apr 18, 2026

    @thomasnalix

    With the increased adoption of Span across the BCL, avoiding intermediate allocations has become a common expectation rather than a micro-optimization.

    BitArray currently requires calling .ToArray() when working with spans, which introduces an unnecessary allocation despite the type already copying internally.

    Given the low implementation cost and consistency with modern APIs, it might be worth reconsidering either:

    • span-based constructors, or
    • a static factory method (Create(ReadOnlySpan)) to limit API surface growth.
  11. added
    api-ready-for-reviewAPI is ready for review, it is NOT ready for implementation
    and removed
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Apr 19, 2026
  12. modified the milestones: Future, 11.0.0 on Apr 19, 2026
  13. tannergooding commented on Apr 19, 2026

    @tannergooding
    Member

    Marked ready-for-review. Simple improvement that avoids an intermediate allocation and copy.

  14. bartonjs commented on May 5, 2026

    @bartonjs
    Member

    Video

    Looks good as proposed

    namespace System.Collections;
    
    public sealed partial class BitArray
    {
        public BitArray(ReadOnlySpan<byte> bytes);
        public BitArray(ReadOnlySpan<bool> values);
        public BitArray(ReadOnlySpan<int> values);
    }
  15. added
    api-approvedAPI was approved in API review, it can be implemented
    and removed
    api-ready-for-reviewAPI is ready for review, it is NOT ready for implementation
    on May 5, 2026
  16. added
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 28, 2026
  17. added a commit that references this issue on Jul 31, 2026
    44687f2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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