Why does a System.Boolean take 4 bytes? It just stores one state, either true or false, which could be stored in less space than 4 bytes.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
A
boolis actually only 1 byte, but alignment may cause 4 bytes to be used on a 32-bit platform, or even 8 bytes on a 64-bit platform. For example, theNullable<bool>(akabool?) type uses a full 32 or 64 bits—depending on platform—even though it’s comprised of just twobools. EDIT: As pointed out by Jon Skeet, padding for alignment isn’t always present. As an example, an array ofNullable<bool>s takes only 2 bytes per object instead of 4 or 8.But even 8 bits to represent a
boolcan be considered wasteful if you have many of them to store. For this reason, if you create a type that has manybools as members, (or uses manyNullable<>types), and users of your class might create many instances of it, you might consider using aBitVector32instead. The framework itself uses this technique to reduce the memory footprint of many of the Windows Forms controls, for instance.