Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 301981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:03:20+00:00 2026-05-12T07:03:20+00:00

Is there a generic BitArray in .NET? I only found the non-generic one. Can

  • 0

Is there a generic BitArray in .NET? I only found the non-generic one.

Can there be a generic BitArray? (i.e. would it be reasonable?)


Edit:

Maybe I should have said type-safe not generic.

Basically when you enumerate the type as object, should it not be int or bool? Or one of them provided in another member enumerator?


Example:

foreach (bool bit in myBitArray)
{

}

Edit:

I just checked the enumerator of the BitArray class, but everything returns an object except .Current property:

public virtual object Current
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:03:21+00:00Added an answer on May 12, 2026 at 7:03 am

    No, there isn’t.

    I’m not even sure what part of a BitArray would be generic if there were one.

    It wouldn’t be hard to create an extension method to take the BitArray and return a bool[] or List<bool> using a for loop on the BitArray. The for loop would not involve boxing since you would be using the BitArray‘s indexer, and the bool[] List<bool> could be enumerated without boxing as well.

    Example extension method:

    static List<bool> ToList( this BitArray ba ) {
        List<bool> l = new List<bool>(ba.Count);
        for ( int i = 0 ; i < ba.Count ; i++ ) {
            l.Add( ba[ i ] );
        }
        return l;
    }
    

    What I found from a quick benchmark (curiosity overcame me) was that foreach (bool b in myBitArray.ToList()) took 75% to 85% of the time that foreach (bool b in myBitArray). That creates the list each time. Creating the list once and iterating over it many times took 20% to 25% of the time that foreach (bool b in myBitArray) took. You could only take advantage of that if you need to iterate over the bool values multiple times and know that they won’t have changed from the time you called myBitArray.ToList().

    foreach (bool b in Enumerable.Cast<bool(myBitArray)) took 150% of the time that foreach (bool b in myBitArray) took.

    Yet another edit: I’d say that since it is a game, it probably does make sense for you to do whatever it takes to have a very lean iteration with no boxing/unboxing, even if that means writing your own BitArray. You could save time and use Reflector to copy most of study BitArray‘s code since the class is sealed (can’t inherit and add functionality), just in case there are bit-twiddling optimizations to learn from.

    Edit: Struck the suggestion to copy code out of Reflector. Some things, like iterators and closures, yield weird generated code that you don’t want to copy directly anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 165k
  • Answers 165k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are a couple of variants of a rename command,… May 12, 2026 at 12:33 pm
  • Editorial Team
    Editorial Team added an answer There is currently no way to build CLS-compliant assemblies from… May 12, 2026 at 12:33 pm
  • Editorial Team
    Editorial Team added an answer You might want to look at Google Protocol Buffers or… May 12, 2026 at 12:33 pm

Related Questions

System.BitArray only implements the non-generic IEnumerable, which returns an Object for the IEnumerator.Current property.
The .Net framework has an Array.Sort overload that allows one to specify the starting
Is there a generic way, without creating and managing your own CLR host, to
Is there a generic API call I can make to get a handle of
Is there a generic way to clone objects in VBA? So that i could

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.