title: sizeof operator gives extra size of a struct in C#
body: <p>I am trying to check size of all of my variables (value types) using sizeof operator. I gone through one of the <a href="https://msdn.microsoft.com/en-in/library/eahchzkf%28v=vs.140%29.aspx" rel="nofollow">msdn article</a> where it is written that </p>
  
  <blockquote>
    <p>For all other types, including structs, the sizeof operator can be used only in unsafe code blocks</p>
  </blockquote>
  
  <p>and also structs should not contain any fields or properties that are reference types</p>
  
  <p>For this, I enabled unsafe compilation in my project properties and created structure as follows-</p>
  
  <pre><code>struct EmployeeStruct
  {
      int empId;
      long salary;       
  }
  </code></pre>
  
  <p>and used it as follows-</p>
  
  <pre><code>unsafe
  {
     size = sizeof(EmployeeStruct);
  }
  
  Console.WriteLine("Size of type in bytes is: {0}", size);
  </code></pre>
  
  <p>Here I am getting output as Size of type in bytes is: 16 however by looking at structure it should be 12 (4 for int and 8 for long).
  Can someone help me understand here that why I am getting 4 byte extra size?</p>