在C#原始大小布尔 [英] Primitive Boolean size in C#

查看:233
本文介绍了在C#原始大小布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#中布尔变量存储在内存中?也就是说,它们是存储为字节和其他7位被浪费,或者,在阵列的情况下,它们是组合成布尔值1字节块

How are boolean variables in C# stored in memory? That is, are they stored as a byte and the other 7 bits are wasted, or, in the case of arrays, are they grouped into 1-byte blocks of booleans?

这回答关于Java的同样的问题(<一个href=\"http://stackoverflow.com/questions/1907318/java-boolean-primitive-type-size\">http://stackoverflow.com/questions/1907318/java-boolean-primitive-type-size).是Java和C#在这方面一样吗?

This answers the same question regarding Java (http://stackoverflow.com/questions/1907318/java-boolean-primitive-type-size). Are Java and C# the same in this regard?

推荐答案

在C#中,肯定的的默认情况下不包装,所以多个布尔的字段的将每取1个字节。您可以使用 BitVector32 BitArray ,或者干脆按位运算来减少这方面的开销。由于的变量的我似乎记得他们采取4个字节(如 INT = 的Int32 基本处理)。

In C#, certainly the bits aren't packed by default, so multiple bool fields will each take 1 byte. You can use BitVector32, BitArray, or simply bitwise arithmetic to reduce this overhead. As variables I seem to recall they take 4 bytes (essentially handled as int = Int32).

例如,下面的套 I 4

struct Foo
{
    public bool A, B, C, D;
}
static unsafe void Main()
{
    int i = sizeof(Foo);
}

这篇关于在C#原始大小布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆