<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[Stuart&#039;s Pixel Games]]></provider_name><provider_url><![CDATA[http://stuartspixelgames.com]]></provider_url><author_name><![CDATA[SPG]]></author_name><author_url><![CDATA[https://stuartspixelgames.com/author/stuie89/]]></author_url><title><![CDATA[How To Use Arrays &#8211;&nbsp;C#]]></title><type><![CDATA[link]]></type><html><![CDATA[<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://www.humblebundle.com/g/puzzledorf"><img data-attachment-id="7638" data-permalink="https://stuartspixelgames.com/wide-ad/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png" data-orig-size="1920,586" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="wide-ad" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=300" data-large-file="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=1024" src="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=736" alt="" class="wp-image-7638" srcset="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=736 736w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=1472 1472w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=150 150w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=300 300w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=768 768w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=1024 1024w" sizes="(max-width: 736px) 100vw, 736px" /></a></figure></div>


<h2 id="what-is-an-array">What Is An Array</h2>



<p>Arrays are very common in game development and something I used frequently in my game <strong><a href="https://www.humblebundle.com/g/puzzledorf">Puzzledorf</a></strong> to store information. One example is that I store the position of every object in the puzzle below in an array, so they&#8217;re useful, especially for creating an undo function.</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://www.humblebundle.com/g/puzzledorf"><img data-attachment-id="6095" data-permalink="https://stuartspixelgames.com/2016/05/15/how-to-set-win-conditions-in-puzzle-script/iceworldgiflevel1/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2016/05/iceworldgiflevel1.gif" data-orig-size="567,408" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="IceWorldGIFLevel1" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2016/05/iceworldgiflevel1.gif?w=300" data-large-file="https://stuartspixelgames.files.wordpress.com/2016/05/iceworldgiflevel1.gif?w=567" src="https://stuartspixelgames.files.wordpress.com/2016/05/iceworldgiflevel1.gif?w=567" alt="" class="wp-image-6095" /></a></figure></div>


<p>The best way to picture an array is to think of it as a series of boxes, all in a row. Each box holds a variable, but all of the same type. Below is an example:</p>



<figure class="wp-block-image"><img data-attachment-id="4903" data-permalink="https://stuartspixelgames.com/2020/08/17/how-to-use-arrays-c/array-example-1/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2020/08/array-example-1-1.png" data-orig-size="179,214" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Array Example 1" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2020/08/array-example-1-1.png?w=179" data-large-file="https://stuartspixelgames.files.wordpress.com/2020/08/array-example-1-1.png?w=179" src="https://stuartspixelgames.files.wordpress.com/2020/08/array-example-1-1.png" alt="Array Example 1" class="wp-image-4903" /></figure>



<p>If we wanted to store an array of&nbsp;integers, or&nbsp;<strong>int&#8217;s</strong>, it might look like this:</p>



<figure class="wp-block-image"><img data-attachment-id="4904" data-permalink="https://stuartspixelgames.com/2020/08/17/how-to-use-arrays-c/int-array-example-1/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-1-2.png" data-orig-size="272,217" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Int Array Example 1" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-1-2.png?w=272" data-large-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-1-2.png?w=272" src="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-1-2.png" alt="Int Array Example 1" class="wp-image-4904" /></figure>



<p>The index is the unique label assigned to each box, in numbered order, and always starting at 0. In the above example, we have 5 boxes, so our array has a&nbsp;<strong>size</strong> of 5, however the labels, or indexes, go from&nbsp;<strong>0 to 4</strong>.</p>



<h2 id="how-to-access-array-elements">How To Access Array Elements</h2>



<p>To access the variable in a particular box (the boxes are more formally referred to as <strong>elements)</strong>&nbsp;we do so like this:</p>



<pre class="wp-block-preformatted"><strong>array[0] = 40;</strong></pre>



<p>The above line of code would change the first box in our array, the box labelled 0, or in proper programming speak, the box with an index of 0, into a value of 40, so that it would be like the example below:</p>



<figure class="wp-block-image"><img data-attachment-id="4905" data-permalink="https://stuartspixelgames.com/2020/08/17/how-to-use-arrays-c/int-array-example-2/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-2.png" data-orig-size="276,220" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Int Array Example 2" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-2.png?w=276" data-large-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-2.png?w=276" src="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-2.png" alt="Int Array Example 2" class="wp-image-4905" /></figure>



<p>Likewise, we can access any box in our array in the same way:</p>



<pre class="wp-block-preformatted"><strong>array[4] = -1;</strong>
<strong>array[3] = 307;</strong>
<strong>array[1] = 208;</strong></pre>



<p>Which would change our array into this:</p>



<figure class="wp-block-image"><img data-attachment-id="4906" data-permalink="https://stuartspixelgames.com/2020/08/17/how-to-use-arrays-c/int-array-example-3/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-3.png" data-orig-size="273,221" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Int Array Example 3" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-3.png?w=273" data-large-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-3.png?w=273" src="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-3.png" alt="Int Array Example 3" class="wp-image-4906" /></figure>



<p>But what if we want to read the information stored in our array and do something with it? We just do this:</p>



<pre class="wp-block-preformatted"><strong>int score = array[3];</strong></pre>



<p>If we did that, our variable <strong>int </strong><strong>score</strong> would be equal to whatever value is stored in box 3 of our array, in this case,&nbsp;<strong>307.</strong></p>



<p>Of course, we couldn&#8217;t do this:</p>



<pre class="wp-block-preformatted"><strong>string highScore = array[3];</strong></pre>



<p>Because the variables in the array in our example are all&nbsp;<strong>int&#8217;s</strong>,<strong>&nbsp;</strong>you can only use them with other&nbsp;<strong>int</strong> variables (unless you do a conversion which is another whole topic).</p>



<p>Furthermore, you can access array elements by using a variable as the index. For example:</p>



<pre class="wp-block-preformatted"><strong>int index = 1;</strong>
<strong>array[index] = 300;</strong></pre>



<p>That would change our array as follows:</p>



<figure class="wp-block-image"><img data-attachment-id="4912" data-permalink="https://stuartspixelgames.com/2020/08/17/how-to-use-arrays-c/int-array-example-3b/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-3b.png" data-orig-size="269,224" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Int Array Example 3b" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-3b.png?w=269" data-large-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-3b.png?w=269" src="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-3b.png" alt="Int Array Example 3b" class="wp-image-4912" /></figure>



<p>You can only use&nbsp;<strong>int</strong> variables as an index. You can&#8217;t use&nbsp;<strong>floats</strong>, unless you convert them to&nbsp;<strong>int&#8217;s.</strong> So you can&#8217;t do:</p>



<pre class="wp-block-preformatted"><strong>float index = 1;</strong>
<strong>array[index] = 300;</strong></pre>



<p>You should get an error saying something like &#8220;Cannot implicitly convert type &#8216;float&#8217; to &#8216;int'&#8221;. In other words, the&nbsp;<strong>index</strong> of an array is always an&nbsp;<strong>int</strong>, and can&#8217;t be otherwise. You could&nbsp;<strong>convert</strong> the float, to an int:</p>



<pre class="wp-block-preformatted"><strong>float index = 1;</strong>
<strong>array[(int)index] = 300;</strong></pre>



<p>But variable type conversion is a whole other subject.</p>



<h2 id="how-to-create-arrays">How To Create Arrays</h2>



<p>When you want to create array, you create it like this:</p>



<pre class="wp-block-preformatted"><strong>int[] scoresArray = new int[10];</strong></pre>



<p>In other words:</p>



<p><strong>dataType[ ] nameOfArray = new dataType[size];</strong></p>



<p>First you have to declare what type of data, or variable, you will be storing in the array. In our example it is the type&nbsp;<strong>int.</strong></p>



<p>The&nbsp;two&nbsp;<strong>[]</strong> after our&nbsp;<strong>data type</strong> means that this will be an array &#8211; it helps to think of them as a little box, like the little boxes that our array is made up of.</p>



<p><strong>string[]&nbsp;</strong>is an array of strings,&nbsp;<strong>float[]&nbsp;</strong>is an array of&nbsp;floats, etc.</p>



<p>Then we name the array, just like with any variable, so we can refer to it later, in this case, <strong>scoresArray.&nbsp;</strong></p>



<p>Then we have to declare that we are making a new array, at which point we repeat what type of data the array is storing, but this time, we put a number inside our box&nbsp;<strong>[size].&nbsp;</strong>This number just means how many boxes we want our array to have. In the above example, we said 10, so it would look like this:</p>



<figure class="wp-block-image"><img data-attachment-id="4907" data-permalink="https://stuartspixelgames.com/2020/08/17/how-to-use-arrays-c/array-size-example-1/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2020/08/array-size-example-1.png" data-orig-size="269,379" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Array Size Example 1" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2020/08/array-size-example-1.png?w=213" data-large-file="https://stuartspixelgames.files.wordpress.com/2020/08/array-size-example-1.png?w=269" src="https://stuartspixelgames.files.wordpress.com/2020/08/array-size-example-1.png" alt="Array Size Example 1" class="wp-image-4907" /></figure>



<p>Remember that arrays start at 0, so although our size is 10, the last box is labelled 9. Each box also says&nbsp;<strong>empty</strong>, because when we create our array, nothing is inside the boxes. The actual value isn&#8217;t empty, but rather it is referred to as&nbsp;<strong>null</strong>.</p>



<figure class="wp-block-image"><img data-attachment-id="4908" data-permalink="https://stuartspixelgames.com/2020/08/17/how-to-use-arrays-c/array-size-example-2/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2020/08/array-size-example-2.png" data-orig-size="282,396" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Array Size Example 2" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2020/08/array-size-example-2.png?w=214" data-large-file="https://stuartspixelgames.files.wordpress.com/2020/08/array-size-example-2.png?w=282" src="https://stuartspixelgames.files.wordpress.com/2020/08/array-size-example-2.png" alt="Array Size Example 2" class="wp-image-4908" /></figure>



<p>If we gave our array a size of something else, like [7], it would look like this:</p>



<p><strong>int[] scores = new int[7];</strong></p>



<figure class="wp-block-image"><img data-attachment-id="4909" data-permalink="https://stuartspixelgames.com/2020/08/17/how-to-use-arrays-c/array-size-example-3/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2020/08/array-size-example-3.png" data-orig-size="289,292" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Array Size Example 3" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2020/08/array-size-example-3.png?w=289" data-large-file="https://stuartspixelgames.files.wordpress.com/2020/08/array-size-example-3.png?w=289" src="https://stuartspixelgames.files.wordpress.com/2020/08/array-size-example-3.png" alt="Array Size Example 3" class="wp-image-4909" /></figure>



<p>You can also fill the array with data at the same time as you create it. There are two ways:</p>



<pre class="wp-block-preformatted"><strong>int[] scoresArray = new int[] {1, 2, 3, 10, 4, 11};</strong></pre>



<p>In the above example, we don&#8217;t declare the size of the array, we just fill it with&nbsp;<strong>int&#8217;s</strong>. This would actually create an array of the same size as the number of&nbsp;<strong>values</strong> that we put into it, in this case, the size would be&nbsp;<strong>6.</strong></p>



<p>We can also do this:</p>



<pre class="wp-block-preformatted"><strong>int[] scoresArray = new int[6] {1, 2, 3, 10, 4, 11};</strong></pre>



<p>We can declare the size of the array and give it values at the same time. The reason you might do it that way is to make sure you don&#8217;t accidentally make the array bigger than intended.</p>



<h2 id="array-bounds">Array Bounds</h2>



<p>You may have wondered what happens if you try and use the index of a box that doesn&#8217;t exist. For example, in the array above, if we did the following:</p>



<pre class="wp-block-preformatted"><strong>scoresArray[9] = 10;</strong></pre>



<p>We would get an error because that box doesn&#8217;t exist. Likewise if we tried:</p>



<pre class="wp-block-preformatted"><strong>scoresArray[-1] = 10;</strong></pre>



<p>We would also get an error because that box doesn&#8217;t exist either, arrays always start at 0.</p>



<p>The most common errors from working with arrays are trying to access a box that isn&#8217;t there.</p>



<h2 id="accessing-empty-elements">Accessing Empty Elements</h2>



<p>Another common error is if you try and access a box in an array that has nothing in it. Take the following array example:</p>



<figure class="wp-block-image"><img data-attachment-id="4910" data-permalink="https://stuartspixelgames.com/2020/08/17/how-to-use-arrays-c/int-array-example-4/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-4.png" data-orig-size="278,292" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Int Array Example 4" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-4.png?w=278" data-large-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-4.png?w=278" src="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-4.png" alt="Int Array Example 4" class="wp-image-4910" /></figure>



<p>If you tried use the variable stored in box <strong>6:&nbsp;</strong></p>



<pre class="wp-block-preformatted"><strong>int highScore = scoresArray[6];</strong></pre>



<p>you would get an error, because it is <strong>null &#8211;&nbsp;</strong>there is no&nbsp;<strong>int</strong> stored in box 6. However, if you tried to store something in box 6, you would get no error:</p>



<pre class="wp-block-preformatted"><strong>scoresArray[6] = 1000;</strong></pre>



<p>Then once you have stored something in the box, you can read from it.</p>



<p>This is why sometimes when people use arrays they will do an&nbsp;<strong>if check</strong> to make sure that the array box, also known as an&nbsp;<strong>array element</strong>, is not&nbsp;<strong>null.&nbsp;</strong>Something like the following:</p>



<pre class="wp-block-preformatted"><strong>if( scoresArray[6] != null)</strong>
<strong>{</strong>
<strong>  highScore = scoresArray[6];</strong>
<strong>}</strong></pre>



<p>If you don&#8217;t understand the&nbsp;<strong>!</strong> symbol, known as <strong>logical NOT,</strong> look at my article on <strong><a href="http://stuartspixelgames.com/2020/08/13/what-are-logical-operators-c/">logical operators</a>.</strong></p>



<h2 id="how-to-get-an-array-s-size">How To Get An Array&#8217;s Size</h2>



<p>If you want to find out how big an array is, that&#8217;s simple:</p>



<pre class="wp-block-preformatted"><strong>int arraySize = arrayName.Length;</strong></pre>



<p>You just call the name of the array, followed by the&nbsp;<strong>Length</strong> property,&nbsp;<strong>arrayName.Length.</strong> This will give you the full size of the array.</p>



<figure class="wp-block-image"><img data-attachment-id="4905" data-permalink="https://stuartspixelgames.com/2020/08/17/how-to-use-arrays-c/int-array-example-2/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-2.png" data-orig-size="276,220" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Int Array Example 2" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-2.png?w=276" data-large-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-2.png?w=276" src="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-2.png" alt="Int Array Example 2" class="wp-image-4905" /></figure>



<p>If you want to access the last box in an array,&nbsp; you can&#8217;t just get the size. If we got the length of the above array, it would be&nbsp;<strong>5.</strong> But there is no box 5, so we would get an error if we tried:</p>



<pre class="wp-block-preformatted"><strong>array[5] = 10;</strong></pre>



<p>So then how can we always be sure to get the last box in our array, without knowing beforehand how big the array is? Simple.</p>



<pre class="wp-block-preformatted"><strong>int lastIndex = array.Length - 1;</strong>
<strong>array[lastIndex] = 10;
</strong></pre>



<p>The last box in array, or the last&nbsp;<strong>element</strong> in an array, will always be one less than the size of the array, or,&nbsp;<strong>array.Length &#8211; 1;</strong></p>



<h2 id="how-to-loop-over-arrays">How To Loop Over Arrays</h2>



<p>If you are not familiar with loops, look <a href="https://www.tutorialspoint.com/csharp/csharp_loops.htm">here.</a></p>



<p>You might want to loop over every element in an array, for example, if you had an array of scores, and you wanted to find the high score, the simplest way would be to use a <strong>for loop</strong>:</p>



<pre class="wp-block-preformatted"><strong>for(int index = 0; index &lt; scoresArray.Length; index++)</strong>
<strong>{</strong>
<strong>&nbsp; &nbsp;if(scoresArray[index] &gt; highScore)</strong>
<strong>   {</strong>
<strong>      highScore = scoresArray[index];
</strong>      <strong>break;</strong>
<strong>   }</strong>
<strong>}</strong></pre>



<p>Remember that to access the last element in an array, you use:</p>



<pre class="wp-block-preformatted"><strong>int lastIndex = array.Length - 1;</strong></pre>



<p>In our loop, we keep increasing&nbsp;<strong>index</strong> while it is&nbsp;<strong>less than</strong> the length of the array, meaning, it will stop at the last&nbsp;<strong>element&nbsp;</strong>in our array, giving us the same value as&nbsp;<strong>lastIndex</strong> above.</p>



<p>The word&nbsp;<strong>break&nbsp;</strong>in the above example will literally break out of the loop, or stop the loop at that point. In the above code example, once we found a score that was higher than our previous high score, the loop would stop (which probably isn&#8217;t what you would really do, you&#8217;d want to compare all scores, but it&#8217;s an example).</p>



<p>You can also use the word&nbsp;<strong>continue</strong> to keep a loop going, but to skip the rest of the code on that iteration through the loop, and start the loop again at the next <strong>index</strong>. Every time you go through a loop, that is considered a single <strong>iteration.&nbsp;</strong>Example:</p>



<pre class="wp-block-preformatted"><strong>for(int index = 0; index &lt; scoresArray.Length; index++)</strong>
<strong>{</strong>
<strong>&nbsp; &nbsp;if(scoresArray[index] &gt; highScore)</strong>
<strong>   {</strong>
<strong>     highScore = scoresArray[index];
</strong>     <strong>continue;</strong>
<strong>   }
</strong>   <strong>Print("No high score found at index " + index);</strong>
<strong>}</strong></pre>



<p>Assuming the function&nbsp;<strong>Print()</strong> prints text to the screen, in the above code example, if we found a new high score, <strong>continue</strong> would stop the text printing to the screen. If you want to see it in action to help you picture it, use the following code sample in a C# console project (Unity example below that):</p>



<pre class="wp-block-preformatted"><strong>int highScore = 100;</strong>
<strong>int[] scoresArray = new int[3];</strong>
<strong>scoresArray[0] = 1;</strong>
<strong>scoresArray[1] = 200;</strong>
<strong>scoresArray[2] = 30;</strong>

<strong>for (int index = 0; index &lt; scoresArray.Length; index++)</strong>
<strong>{</strong>
<strong>  if (scoresArray[index] &gt; highScore)</strong>
<strong>  {</strong>
<strong>    highScore = scoresArray[index];</strong>
<strong>    continue;</strong>
<strong>  }</strong>
<strong>  Console.WriteLine("No high score found at index " + index);</strong>
<strong>}
</strong>
<strong>Console.ReadKey();</strong></pre>



<p><strong>Console.WriteLine()</strong> will print text, <strong>Console.ReadKey()</strong> will pause the application until a key is pressed on the keyboard. You can also use <strong>Console.Read()</strong> to completely halt the application.</p>



<p>If you want to test it in Unity, get rid of <strong>Console.ReadKey()</strong> and replace <strong>Console.WriteLine()&nbsp;</strong>with&nbsp;<strong>Debug.Log(&#8220;Text here&#8221;);</strong></p>



<p>You will print out:</p>



<pre class="wp-block-preformatted"><strong>No high score found at index 0</strong>
<strong>No high score found at index 2</strong></pre>



<p>It would skip <strong>index 1</strong>, because of&nbsp;<strong>continue.</strong></p>



<h2 id="how-to-copy-and-duplicate-arrays">How To Copy And Duplicate Arrays</h2>



<p>It&#8217;s no simple task to copy an array. You can&#8217;t just do:</p>



<pre class="wp-block-preformatted"><strong>array2 = array1;</strong></pre>



<p>I will demonstrate why. Try the following code sample:</p>



<pre class="wp-block-preformatted"><strong>string[] creatureNames1 = new string[4] { "Dragon", "Orc"</strong><strong>, 
"Mammoth" ,"Slime" };</strong>
<strong>string[] creatureNames2 = creatureNames1;</strong>
<strong>creatureNames2[0] = "Dire Wolf";</strong>

<strong>Console.WriteLine("Creature Names 2:");</strong>
<strong>Console.WriteLine(creatureNames2[0]);</strong>
<strong>Console.WriteLine(creatureNames2[1]);</strong>
<strong>Console.WriteLine(creatureNames2[2]);</strong>
<strong>Console.WriteLine(creatureNames2[3]);</strong>

<strong>Console.WriteLine("");</strong>
<strong>Console.WriteLine("Creature Names 1:");</strong>
<strong>Console.WriteLine(creatureNames1[0]);</strong>
<strong>Console.WriteLine(creatureNames1[1]);</strong>
<strong>Console.WriteLine(creatureNames1[2]);</strong>
<strong>Console.WriteLine(creatureNames1[3]);</strong>

<strong>Console.ReadKey();</strong></pre>



<p>Don&#8217;t forget to swap our&nbsp;<strong>Console</strong> commands for&nbsp;<strong>Debug.Log</strong> in Unity.</p>



<p>If you run that code, you will see that when we change element [0] in <strong>creatureNames2 </strong>from &#8220;Dragon&#8221; to &#8220;Dire Wolf&#8221;, it changes element [0] in both arrays.</p>



<p>This is because when you create an array, each element has a specific place it is stored in the devices memory, a place that has a specific address, eg, 1x000AC or something like that. It&#8217;s just like your home address.</p>



<p>When you say&nbsp;<strong>array1 = array2</strong>, rather than copying the information from one array over to the other, you are in fact copying the memory addresses of where information is stored in <strong>array1</strong> into&nbsp;<strong>array2.&nbsp;</strong></p>



<p>This is because <strong>array1</strong> and <strong>array2</strong> are not in fact actual copies of the array, but references to where the array is stored in memory &#8211; they are more like address labels to stick on an envelope, containing the address of where you live, but your house is actually somewhere else.</p>



<p>It&#8217;s like a Google Doc &#8211; a document that is online, up in the cloud, and sharing it with two friends. They can both work on it at the same time, online, on two different computers, but if they make changes, they are both changing the same document.</p>



<p>The solution is this:</p>



<pre class="wp-block-preformatted"><strong>string[] creatureNames1 = new string[4] { "Dragon", "Orc", 
"Mammoth" ,"Slime" };</strong>
<strong>string[] creatureNames2 = new string[4];</strong>

<strong>for(int index = 0; index &lt; creatureNames1.Length; index++)</strong>
<strong>{</strong>
<strong>  creatureNames2[index] = creatureNames1[index];</strong>
<strong>}</strong>

<strong>Console.ReadKey();</strong></pre>



<p>First of all, we create our&nbsp;<strong>creatureNames1</strong> array.&nbsp;Then we create our <strong>creatureNames2</strong> array.</p>



<p>When we use the word&nbsp;<strong>new</strong>, we are in fact saying that we want to store this array at a&nbsp;<strong>new memory address.</strong> So at the moment,&nbsp;<strong>creatureNames2</strong> is an array that is stored at it&#8217;s own unique address. As soon as we go:</p>



<pre class="wp-block-preformatted"><strong>creatureNames2 = creatureNames1;</strong></pre>



<p>If we did that, <strong>creatureNames2</strong> would no longer have a unique address, but will have the same array address as <strong>creatureNames1,&nbsp;</strong>because we are copying the arrays address, not the array.</p>



<p>This is why instead we create a loop, and loop over every element of both arrays, and uniquely assign each element of&nbsp;<strong>creatureNames1</strong> to the corresponding&nbsp;element in&nbsp;<strong>creatureNames2</strong> through using the same&nbsp;<strong>index.</strong> When we do this,&nbsp;<strong>creatureNames2</strong> still has its own unique address, but it now has the same information as&nbsp;<strong>creatureNames1.</strong></p>



<h2 id="how-to-resize-arrays">How To Resize Arrays</h2>



<p>If you expect to be resizing your arrays, you&#8217;re better off using an alternative to arrays like&nbsp;<strong>Lists</strong> which do it all for you. However, below is how you resize an array.</p>



<p>Once an arrays size is set, you can&#8217;t easily change it manually. You in fact would have to copy the array elements first to a temporary duplicate array, then resize the array and copy the elements back. Example:</p>



<pre class="wp-block-preformatted"><strong>string[] creatureNames1 = new string[4] { "Dragon", "Orc", 
"Mammoth", "Slime" };</strong>
<strong>string[] oldCreatureNames = new string[creatureNames1.Length];</strong>

<strong>// COPY OLD NAMES TO DUPLICATE ARRAY</strong>
<strong>for (int index = 0; index &lt; creatureNames1.Length; index++)</strong>
<strong>{</strong>
<strong>  oldCreatureNames[index] = creatureNames1[index];</strong>
<strong>}</strong>

<strong>// RESIZE</strong>
<strong>creatureNames1 = new string[6]; </strong>

<strong>// COPY NAMES BACK TO RESIZED ARRAY</strong>
<strong>for (int index = 0; index &lt; oldCreatureNames.Length; index++)</strong>
<strong>{</strong>
  <strong>creatureNames1[index] = oldCreatureNames[index];</strong>
<strong>}</strong>

<strong>// ADD NEW NAMES</strong> 
<strong>creatureNames1[4] = "Baboon";</strong>
<strong>creatureNames1[5] = "Dire Wold";</strong>

<strong>Console.WriteLine("");</strong>
<strong>Console.WriteLine("Creature Names 1:");</strong>
<strong>Console.WriteLine(creatureNames1[0]);</strong>
<strong>Console.WriteLine(creatureNames1[1]);</strong>
<strong>Console.WriteLine(creatureNames1[2]);</strong>
<strong>Console.WriteLine(creatureNames1[3]);</strong>
<strong>Console.WriteLine(creatureNames1[4]);</strong>
<strong>Console.WriteLine(creatureNames1[5]);</strong>

<strong>Console.ReadKey();</strong></pre>



<p>I put some comments in but: First we create our initial array&nbsp;<strong>creatureNames1</strong>, then we create a duplicate array to store all of the old names called&nbsp;<strong>oldCreatureNames.</strong></p>



<p>We then loop over&nbsp;<strong>oldCreatureNames</strong> to copy the names over from&nbsp;<strong>creatureNames1.</strong></p>



<p>We then use&nbsp;<strong>new</strong> again on&nbsp;<strong>creatureNames1</strong> to create a brand new array, stored at a different memory address, that is now bigger than what it was previously. It now has a length of&nbsp;<strong>6&nbsp;</strong>rather than&nbsp;<strong>4.</strong></p>



<p>We then copy all of the old names back into the now resized&nbsp;<strong>creatureNames1</strong> array.</p>



<p>Finally we add two new creature names to the array and print them all out.</p>



<h2 id="what-are-multi-dimensional-arrays">What Are Multi-dimensional Arrays</h2>



<p>So far what we have been looking at are called one dimensional arrays, or 1D arrays. But there are also multi-dimensional arrays, most commonly, 2D and 3D arrays.</p>



<p>A 1D array is just a single row of boxes, as we&#8217;ve looked at:</p>



<figure class="wp-block-image"><img data-attachment-id="4904" data-permalink="https://stuartspixelgames.com/2020/08/17/how-to-use-arrays-c/int-array-example-1/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-1-2.png" data-orig-size="272,217" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Int Array Example 1" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-1-2.png?w=272" data-large-file="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-1-2.png?w=272" src="https://stuartspixelgames.files.wordpress.com/2020/08/int-array-example-1-2.png" alt="Int Array Example 1" class="wp-image-4904" /></figure>



<p>A 2D array, however, looks more like an except spread sheet; it has rows and columns, like this:</p>



<figure class="wp-block-image"><img data-attachment-id="4954" data-permalink="https://stuartspixelgames.com/2020/08/17/how-to-use-arrays-c/2d-array-example-1/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2020/08/2d-array-example-1-3.png" data-orig-size="490,128" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="2D array example 1" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2020/08/2d-array-example-1-3.png?w=300" data-large-file="https://stuartspixelgames.files.wordpress.com/2020/08/2d-array-example-1-3.png?w=490" src="https://stuartspixelgames.files.wordpress.com/2020/08/2d-array-example-1-3.png" alt="2D array example 1" class="wp-image-4954" /></figure>



<p>In the above example, we have a 2D array of&nbsp;<strong>strings</strong> which are monster names. Generally these are reffered to as X index for Rows, and Y index for Columns.</p>



<p>The best way to think of a 3D array is like a Rubix Cube, or a series of 2D arrays. It has both rows and columns, which is the X and Y, but also a Z index, which puts one 2D array below another. Below is an example of a 3D array:</p>



<figure class="wp-block-image"><img data-attachment-id="4946" data-permalink="https://stuartspixelgames.com/2020/08/17/how-to-use-arrays-c/3d-array-example/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2020/08/3d-array-example-4.png" data-orig-size="667,868" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="3D Array Example" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2020/08/3d-array-example-4.png?w=231" data-large-file="https://stuartspixelgames.files.wordpress.com/2020/08/3d-array-example-4.png?w=667" src="https://stuartspixelgames.files.wordpress.com/2020/08/3d-array-example-4.png" alt="3D Array Example" class="wp-image-4946" /></figure>



<p>What looks like multiple 2D arrays is actually a single 3D array. It is a shop inventory, and each different 2D array represents a type of item that the shop has in stock. Each separate 2D array is stored on a&nbsp;<strong>Z index</strong>, which I have labelled&nbsp;<strong>Z 0, Z 1, Z 2</strong> to make it clearer.</p>



<p>I&#8217;ll go into a bit more detail on 2D and 3D arrays below.</p>



<h2 id="how-to-create-a-2d-array">How To Create A 2D Array</h2>



<p>You create a 2D array like follows:</p>



<pre class="wp-block-preformatted"><strong>string[,] monsterNames = new string[3,3];</strong></pre>



<p>You can see how that is similar to declaring 1D arrays, except this time we have a comma in the middle. Left of the comma is <strong>X</strong> for <strong>rows</strong>, and the right is <strong>Y</strong> for <strong>columns,<br></strong>or <strong>[x, y]</strong>.<strong>&nbsp;</strong>The above line of code would create a 2D array like our above examples, except it would be empty:</p>



<figure class="wp-block-image"><img data-attachment-id="4929" data-permalink="https://stuartspixelgames.com/2020/08/17/how-to-use-arrays-c/2d-array-example-2/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2020/08/2d-array-example-2.png" data-orig-size="479,132" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="2D array example 2" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2020/08/2d-array-example-2.png?w=300" data-large-file="https://stuartspixelgames.files.wordpress.com/2020/08/2d-array-example-2.png?w=479" src="https://stuartspixelgames.files.wordpress.com/2020/08/2d-array-example-2.png" alt="2D array example 2" class="wp-image-4929" /></figure>



<p>We can fill the array when we create it like so:</p>



<pre class="wp-block-preformatted"><strong>string[,] monsterNames = new string[2, 2] { { "Dragon", "Wolf" }, </strong>
<strong>{ "Slime", "Goblin" } };</strong></pre>



<p>Above is one line of code. It would give us the following array:</p>



<figure class="wp-block-image"><img data-attachment-id="4955" data-permalink="https://stuartspixelgames.com/2020/08/17/how-to-use-arrays-c/2d-array-example-3/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2020/08/2d-array-example-3-1.png" data-orig-size="343,95" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="2D array example 3" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2020/08/2d-array-example-3-1.png?w=300" data-large-file="https://stuartspixelgames.files.wordpress.com/2020/08/2d-array-example-3-1.png?w=343" src="https://stuartspixelgames.files.wordpress.com/2020/08/2d-array-example-3-1.png" alt="2D array example 3" class="wp-image-4955" /></figure>



<p>[2, 2] means <strong>x&nbsp;</strong>has a size of 2, and <strong>y</strong> has a size of 2. You could also declare it without declaring the size, just giving it values:</p>



<pre class="wp-block-preformatted"><strong>string[,] monsterNames = new string[,] { { "Dragon", "Wolf" }, </strong>
<strong>{ "Slime", "Goblin" } };</strong></pre>



<p>C# will work out how many values you put into the array, and adjust the size accordingly. If you want to try it out, you can test this code in your C# console project:</p>



<pre class="wp-block-preformatted"><strong>string[,] monsterNames = new string[2,2] { { "Dragon", "Wolf" }, </strong>
<strong>{ "Slime", "Goblin" } };

Console.WriteLine(monsterNames[0, 0]);
Console.WriteLine(monsterNames[0, 1]);
Console.WriteLine(monsterNames[1, 0]);
Console.WriteLine(monsterNames[1, 1]);
Console.ReadKey();</strong></pre>



<p>Again, to test it in Unity, replace&nbsp;<strong>Console.WriteLine</strong> with&nbsp;<strong>Debug.Log&nbsp;</strong>and remove&nbsp;<strong>Console.ReadKey().</strong></p>



<p>If you test it, it will print out:</p>



<pre class="wp-block-preformatted"><strong>Dragon</strong>
<strong>Wolf</strong>
<strong>Slime</strong>
<strong>Goblin</strong></pre>



<p>So you can see that when you declare initial values in a 2D array, it assigns them to each row at a time, starting at <strong>X index</strong> <strong>0</strong> and&nbsp;<strong> Y Index 0</strong>, finishing at&nbsp;<strong>X index 0</strong> and <strong>Y index End,</strong> that being an entire row, then moving on to the next row, starting at&nbsp;<strong>X index 1&nbsp;</strong>and&nbsp;<strong>Y index 0, </strong>etc.</p>



<h2 id="how-to-access-elements-in-a-2d-array">How To Access Elements in a 2D Array</h2>



<p>As the above code example demonstrated, you access the elements in a 2D array in almost the same way as with a 1D array, except you have to specify both the X and Y index in the array.</p>



<figure class="wp-block-image"><img data-attachment-id="4955" data-permalink="https://stuartspixelgames.com/2020/08/17/how-to-use-arrays-c/2d-array-example-3/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2020/08/2d-array-example-3-1.png" data-orig-size="343,95" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="2D array example 3" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2020/08/2d-array-example-3-1.png?w=300" data-large-file="https://stuartspixelgames.files.wordpress.com/2020/08/2d-array-example-3-1.png?w=343" src="https://stuartspixelgames.files.wordpress.com/2020/08/2d-array-example-3-1.png" alt="2D array example 3" class="wp-image-4955" /></figure>



<p>You can see the relative indexes above. To get the cat:</p>



<pre class="wp-block-preformatted"><strong>monsterNames[0, 0];</strong></pre>



<p>To get the Hamster:</p>



<pre class="wp-block-preformatted"><strong>monsterNames[1, 1];
</strong></pre>



<h2 id="how-to-create-a-3d-array">How To Create A 3D Array</h2>



<p>You create a 3D array in a similar way to 2D arrays. Let&#8217;s look at creating a shop inventory again.</p>



<pre class="wp-block-preformatted"><strong>string[, ,] shopInventory = new string[3, 2 , 2];</strong></pre>



<p>That would give us an empty 3D array in the order <strong>[z, x, y]</strong>. To prefill it:</p>



<pre class="wp-block-preformatted"><strong>string[,,] shopInventory = new string[3, 2, 2]</strong>
<strong><span style="color:var(--color-text);font-size:1rem;">{&nbsp;
</span>  {{ "Sword", "Axe"} , { "Club", "Sickle"}},</strong>
  <strong>{{"Small Potion", "Large Potion"}, {"Strength Potion", "Defense Potion"}}, </strong>
  <strong>{{ "Melon", "Berry"} , { "Nut", "Juice"}} </strong>
<strong>};</strong></pre>



<p>It works similar to the 2D array, with with an extra layer of&nbsp;<strong>{ }</strong> curly braces. Remember that 2D arrays fill in order of each row of X at a time, ie, x-0 y-0, x-0 y-1, etc. 3D arrays fill the same way, but starting with z-0, then z-1, then z-2, etc.</p>



<p>The first outer&nbsp;<strong>{ }</strong> are to signify the bounds of the array.</p>



<p>Then each line in the above code example signifies one&nbsp;<strong>z&nbsp;</strong><strong>index</strong>, with the&nbsp;<strong>{}</strong> braces meaning&nbsp;<strong>z-&#8230;</strong>, and the inner sets of curly braces signifying each&nbsp;<strong>x-row</strong>.</p>



<p>Here&#8217;s a picture of what the above code creates:</p>



<figure class="wp-block-image"><img data-attachment-id="4945" data-permalink="https://stuartspixelgames.com/2020/08/17/how-to-use-arrays-c/3d-array-example-2/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2020/08/3d-array-example-2-1.png" data-orig-size="424,518" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="3D Array Example 2" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2020/08/3d-array-example-2-1.png?w=246" data-large-file="https://stuartspixelgames.files.wordpress.com/2020/08/3d-array-example-2-1.png?w=424" src="https://stuartspixelgames.files.wordpress.com/2020/08/3d-array-example-2-1.png" alt="3D Array Example 2" class="wp-image-4945" /></figure>



<h2 id="accessing-elements-of-a-3d-array">Accessing Elements of a 3D Array</h2>



<p>You can print it out for yourself to see:</p>



<pre class="wp-block-preformatted"><strong>// PRINT</strong>
<strong>Console.WriteLine("Weapons / Z 0: ");</strong>
<strong>Console.WriteLine(shopInventory[0, 0, 0] +", " + shopInventory[0, 0, 1]);</strong>
<strong>Console.WriteLine(shopInventory[0, 1, 0] + ", " + shopInventory[0, 1, 1]);</strong>

<strong>Console.WriteLine("");</strong>
<strong>Console.WriteLine("Potions / Z 1: ");</strong>
<strong>Console.WriteLine(shopInventory[1, 0, 0] + ", " + shopInventory[1, 0, 1]);</strong>
<strong>Console.WriteLine(shopInventory[1, 1, 0] + ", " + shopInventory[1, 1, 1]);</strong>

<strong>Console.WriteLine("");</strong>
<strong>Console.WriteLine("Food / Z 2: ");</strong>
<strong>Console.WriteLine(shopInventory[2, 0, 0] + ", " + shopInventory[2, 0, 1]);</strong>
<strong>Console.WriteLine(shopInventory[2, 1, 0] + ", " + shopInventory[2, 1, 1]);</strong>
<strong>Console.Read();</strong></pre>



<p>We access the elements in a 3D array the same as other arrays, just remembering to do it in the order <strong>[z, x, y]</strong>.</p>



<h2 id="looping-over-2d-and-3d-arrays">Looping Over 2D and 3D Arrays</h2>



<p>You loop over 2D and 3D arrays in the same way you do with 1D arrays, you just have to nest one loop inside another. Here is an example to print out our shop inventory but with loops:</p>



<pre class="wp-block-preformatted"><strong>const int zSize = 3;</strong>
<strong>const int xSize = 2;</strong>
<strong>const int ySize = 2;</strong>

<strong>//3D Array</strong>
<strong>string[,,] shopInventory = new string[zSize, xSize, ySize] 
{ </strong>
<strong>{{ "Sword", "Axe"} , { "Club", "Sickle"}},</strong>
<strong>{{"Small Potion", "Large Potion"}, {"Strength Potion", "Defense Potion"}}, </strong>
<strong>{{ "Melon", "Berry"} , { "Nut", "Juice"}} 
};</strong>

<strong>for(int z = 0; z &lt; zSize; z++)</strong>
<strong>{</strong>
<strong>  for(int x = 0; x &lt; xSize; x++)</strong>
<strong>  {</strong>
<strong>    for(int y = 0; y &lt; ySize; y++)</strong>
<strong>    {</strong>
<strong>      Console.WriteLine(shopInventory[z, x, y]);</strong>
<strong>    }</strong>
<strong>  }</strong>
<strong>}</strong>

<strong>Console.Read();</strong></pre>



<p>We use a&nbsp;<strong>const int</strong> to set the size of our array this time. A <strong>const </strong>variable is one that never changes. You can&#8217;t use a regular <strong>int</strong> to set the size of an array, because <strong>int</strong><strong>&#8216;s </strong>can change, and the size of the array can&#8217;t. It&#8217;s also better coding practice to use variables to set the array size, because we can re-use those values elsewhere in our code, like working out how many times to run our loop.</p>



<p>We then run 3 nested loops: we start looping over the&nbsp;<strong>z index</strong> of our array,<strong>&nbsp;</strong>then the <strong>y index</strong>, then the&nbsp;<strong>x index</strong>. This will print out every element in our 3D array in order.</p>



<p>You have to loop over arrays in the same order that you input the values, meaning, we input our values in&nbsp;<strong>[z, x, y]&nbsp;</strong>so we have to loop in that order,&nbsp;<strong>[z, x, y]</strong>. If you tried looping in some other order like&nbsp;<strong>[x, y, z]</strong>,&nbsp; you would get errors, going out of bounds of the array and trying to access things that aren&#8217;t there.</p>



<h2 id="other-types-of-arrays">Other Types of Arrays</h2>



<p>There are other types of arrays, such as jagged arrays, Lists and unordered maps, but they are covered elsewhere.</p>



<div class="is-layout-flow wp-block-group"><div class="wp-block-group__inner-container">
<p class="has-text-align-center has-medium-font-size"><strong>If you enjoyed reading, try my game <a href="https://www.humblebundle.com/g/puzzledorf">Puzzledorf</a>.</strong></p>
</div></div>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://www.humblebundle.com/g/puzzledorf"><img data-attachment-id="7638" data-permalink="https://stuartspixelgames.com/wide-ad/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png" data-orig-size="1920,586" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="wide-ad" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=300" data-large-file="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=1024" src="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=736" alt="" class="wp-image-7638" srcset="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=736 736w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=1472 1472w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=150 150w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=300 300w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=768 768w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=1024 1024w" sizes="(max-width: 736px) 100vw, 736px" /></a></figure></div>]]></html><thumbnail_url><![CDATA[https://stuartspixelgames.files.wordpress.com/2017/03/c.png?fit=440%2C330]]></thumbnail_url><thumbnail_width><![CDATA[415]]></thumbnail_width><thumbnail_height><![CDATA[255]]></thumbnail_height></oembed>