Tony said:
Hi!
Is it anyone that might have a good explanation why the designor of .NET
made a bool 4 bytes.
I mean it's just a wast of memory.
For a variety of reasons. I would guess that the most important is that
there's no compelling reason for it _not_ to be 4 bytes, and making it 4
bytes keeps it consistent with other data types.
While 4 bytes obviously takes up more room than 1 byte, the fact is that
large arrays of booleans aren't really all that common, and there's
already a 1 byte type anyway (it's called 'byte'…duh
). On the other
hand, people generally do care about efficient access to booleans, as
well as preserving atomic semantics in multi-threaded scenarios.
On current hardware architectures, it's more efficient to move 32 bits
around than just 8. And because of that, there's the possibility that
writing to a 1-byte boolean would require first reading 4 bytes,
modifying a single byte within those 4 bytes, and then writing the 4
bytes back. Obviously that's harder for .NET to make atomic than just
writing to a single 32-bit word, and of course the efficiency aspect
alone is a decent enough reason.
When you think about it, you might as well ask why a boolean isn't just
1 _bit_ in size. The issues are actually quite similar.
Pete