In this lecture, I'll show you why we start our array indexes at zero. To do that, I have to use some hideous code that I've never had to use in my entire professional game development career. So don't write code like this. I just need to use code like this to show you why we start our array indexes at zero. With that big buildup, let's go take a look. This is the hideous code we're going to look at. Although the first part isn't actually hideous, it teaches you something new about arrays. Here I've declared an integer array called scores. Instead of saying equal new int, square bracket, five, close square brackets, and then doing something to initialize the contents of the array, instead I'm initializing the array by putting the values for the array in-between this open curly brace and this close curly brace. Given the syntax, the compiler can figure out that I want an array object of five element so it creates a new array object of five elements and initializes the array with these values into those elements. So if you know ahead of time what initial values you want in your array, you can use this syntax to both create the array object and initialize the array with those element values. Now we get down to the ugly stuff. I've got to say, I have never had to use any of this in any of the commercial games I've worked on in the past or the ones I'm working on now, I had to teach myself how to do this unsafe code just to show you why we start at zero in this video. So don't go home and say, Dr. Teach showed me how to do this unsafe stuff and said that's how you should do it. This is absolutely not how you should do it. I'm only doing it this way because I would need to get at actual memory addresses to show you how this works. In C#, we use references and let the runtime system figure out the memory addresses that go with those references, but here I need to actually skip that step and get right at the addresses. So don't do it this way. I had to mark this code as unsafe. I had to go change the configuration of the project so it lets me use unsafe code, and I'm not even going to talk about how the code actually works. What I'm going to do is just run it, and here's the important stuff. We have our scores array and I've listed each element in the array. The zeroth element scores of zero is at this memory address and it holds 100. You can scan down this right-hand column and you see that the array was initialized properly. Let's talk about why we actually start at zero. As you can see, scores zero starts at the memory location that has this big, long number but ends in 72, and then scores one starts at the memory location that ends in 76, and then scores two starts at the memory location that ends with 80, and so on. Why does this help us explain why we start at zero? It helps us because you can see each of these go up by four. We know in C# that the int datatype takes four bytes, so each element of the array takes four bytes. That's no surprise at all. We start indexing at zero because knowing the starting address of the array and knowing what index we want to look at and knowing the size of each element, the runtime system can do some really fast math to calculate the memory location it needs to go to. Let's do an example with index 3, so the fourth element in the array. What the runtime system does is it takes this memory address, then it adds 3 times 4, the index times the element size. 3 times 4 is 12, 72 plus 12 is 84, and that's exactly where the scores three element starts in memory. If we start indexing at zero, the runtime system can do really fast math, knowing those three things: the starting address of the array, the index of the element we're trying to access, and the size of each element. The runtime system can do really fast math to calculate the memory address to go to. If we started indexing at one instead, the runtime system would always have to take the index where you're trying to access, subtract one from it, and multiply it by the size of each element and add it to the starting address of the array. Why do an extra subtraction every time you're accessing an array element, if you don't need to, we avoid that by starting indexing at zero instead. I'm going to convert this code to use shorts instead of ints and then run the code. As you can see here, this time, the memory addresses go up by two instead of by four because shorts in C# are two bytes, not four bytes like ints are. This works no matter what the datatype you have for the elements in your array. This starting at zero and doing the base address or the starting address of the array plus the index of the element you're trying to access multiplied by the size of each element, that works no matter what data type you have in the array, and that's why we start indexing at zero for our arrays. To recap, in this lecture, you learned that we start our array indexes at zero so we can use the index to calculate an offset from the base address or the starting address of the first element in the array.