title: C# array of objects, very large, looking for a better way
body: <p>Ok, so In one of my projects, im trying to remake the way it stores certain variables, i have a simple array of objects. The class that these objects refer to is:</p>
  
  <pre><code>class Blocks
  {
      public byte type = Block.Empty;
      byte lastblock = Block.Zero;
  }
  </code></pre>
  
  <p>I plan to add more to it, in the current class type is what the objects current value is, and lastblock is what the object used to be.</p>
  
  <p>i create the array like this:</p>
  
  <pre><code>blocks = new Blocks[width * depth * length];
  
  for (int i = 0; i &lt; ((width * length) * depth); i++)
  {
      blocks[i] = new Blocks();
  }
  </code></pre>
  
  <p>The problem that I'm having, is that when i create an array that's very large (512,512,512 or 134217728 for those of you whom don't like math), the array gets huge, upwards of 3.5 gigs. </p>
  
  <p>The old way that this array was created was a little simpler, but much harder to expand, it simple created an array of bytes representing the current block, and seems to only use 2 megs of ram when its actually loaded (which i dont get since 134217728 bytes should be around 134 megs... right?). It just baffles me that object references could generate that much more ram usage.</p>
  
  <p>Am i doing something wrong, or should i just go back to the old way it was done? I would like the object references simply because it means that all my variables are in 1 array rather then in 4 separate arrays, which <em>seems</em> as though it would be better for the system.</p>
  
  <p>EDIT:</p>
  
  <p>After working through a few different ways of doing this, i found that changing</p>
  
  <pre><code>class Blocks
  </code></pre>
  
  <p>to </p>
  
  <pre><code>struct Blocks
  </code></pre>
  
  <p>Made a world of difference, Thank you community for that welcome tip for future use, unfortunately i didn't want to just add two bytes in a struct and call it done, that was where i stopped to test my design and wound up with the original problem. After adding anything else to the struct (anything else that's on my list at least, meaning either a player object reference or a player name string.) It causes an out-of-memory exception that means I'm not going to be able to use this system after all.</p>
  
  <p>But the knowledge of how to do it will be quite helpful in the future. For that, thank you again.</p>