title: What is the size of a boolean In C#? Does it really take 4-bytes?
body: <p>I have two structs with arrays of bytes and booleans:</p>
  
  <pre><code>using System.Runtime.InteropServices;
  
  [StructLayout(LayoutKind.Sequential, Pack = 4)]
  struct struct1
  {
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
      public byte[] values;
  }
  
  [StructLayout(LayoutKind.Sequential, Pack = 4)]
  struct struct2
  {
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
      public bool[] values;
  }
  </code></pre>
  
  <p>And the following code:</p>
  
  <pre><code>class main
  {
      public static void Main()
      {
          Console.WriteLine("sizeof array of bytes: "+Marshal.SizeOf(typeof(struct1)));
          Console.WriteLine("sizeof array of bools: " + Marshal.SizeOf(typeof(struct2)));
          Console.ReadKey();
      }
  }
  </code></pre>
  
  <p>That gives me the following output:</p>
  
  <pre><code>sizeof array of bytes: 3
  sizeof array of bools: 12
  </code></pre>
  
  <p>It seems to be that a <code>boolean</code> takes 4 bytes of storage. Ideally a <code>boolean</code>  would only take one bit (<code>false</code> or <code>true</code>, <code>0</code> or <code>1</code>, etc..).</p>
  
  <p>What is happening here? Is the <code>boolean</code> type really so inefficient?</p>