Finding all adjacent items in 3D list
<p>I have a 3D list (3 nested lists), and for each item in the list, I need to find the adjacent items. Currently, I'm creating an array for each adjacent item, and storing that in each item. This is pretty resource intensive, so I'm looking to simplify it a bit.</p>

<p>I was considering using this approach:</p>

<pre><code>for (int i = 0; i &lt; 8; i++)
{
    switch (i)
    {
        case (0):
            mapCell neighbor = myMap.Depth[z].Rows[x].Columns[y - 1];
        case (1):
            mapCell neighbor = myMap.Depth[z].Rows[x].Columns[y + 1];
        case (2):
            mapCell neighbor = myMap.Depth[z].Rows[x - 1].Columns[y];
        case (3):
            mapCell neighbor = myMap.Depth[z].Rows[x + 1].Columns[y];
        case (4):
            mapCell neighbor = myMap.Depth[z].Rows[x + 1].Columns[y + 1];
        case (5):
            mapCell neighbor = myMap.Depth[z].Rows[x + 1].Columns[y - 1];
        case (6):
            mapCell neighbor = myMap.Depth[z].Rows[x - 1].Columns[y + 1];
        case (7):
            mapCell neighbor = myMap.Depth[z].Rows[x - 1].Columns[y - 1];
    }

    // do some stuff with the neighbor here
}
</code></pre>

<p>This is a lot better than what I have now in terms of performance, but I'm wondering if there is an easier way to accomplish this? It seems a bit messy, and I really feel like there would be a way to do this in 1 line, but I just cant figure out the math.</p>

<p><strong>edit</strong>: Sorry, I may have omitted some important details. The code which works with the "neighbor" variable has been left out, as its very long and doesnt help with the solution. I do not need to maintain a list of the "neighbor" variables (Thats what I am currently doing, and it uses far too much memory (around 400megs). I just need a quick way to look at each item, find each of the adjacent 8 items, do something with them one at a time, and then move onto the next node. The above code will work to go through them, but it doesnt feel like the most optimized way to accomplish this.</p>
