1 00:00:00,000 --> 00:00:04,671 [MUSIC] 2 00:00:04,671 --> 00:00:08,922 Up until now we've been dealing with individual objects. 3 00:00:08,922 --> 00:00:13,150 Tree house Defense will need to be able to deal with groups of objects. 4 00:00:13,150 --> 00:00:16,980 For example, we'll have many towers and many invaders. 5 00:00:16,980 --> 00:00:20,680 It'd be pretty difficult to keep track of all of these objects if we had to create 6 00:00:20,680 --> 00:00:24,910 a separate variable for each tower and give each variable a different name. 7 00:00:25,930 --> 00:00:30,390 What we need is to be able to keep track of them as a collection of objects. 8 00:00:31,580 --> 00:00:34,650 There's a lot of ways to represent a collection of objects. 9 00:00:34,650 --> 00:00:35,899 The simplest is an array. 10 00:00:37,070 --> 00:00:40,600 You can think of an array as a numbered list of items, 11 00:00:40,600 --> 00:00:44,700 we refer to this number as an items Index in the array. 12 00:00:44,700 --> 00:00:49,700 This allows us to identify each item based on its location in the list. 13 00:00:49,700 --> 00:00:53,000 For example, we can say we want the third item in the list. 14 00:00:54,380 --> 00:00:58,700 Remember how we learned earlier if we start counting at zero in programming. 15 00:00:58,700 --> 00:01:02,060 Well, we do that with arrays and other collections, too. 16 00:01:02,060 --> 00:01:06,230 So the third item in the list actually has an index of 2. 17 00:01:06,230 --> 00:01:09,210 Let's learn how to work with arrays in C Sharp. 18 00:01:10,700 --> 00:01:13,830 To learn about arrays, let's open the C Sharp Rebel. 19 00:01:13,830 --> 00:01:18,780 To open the C Sharp Rebel in workspaces, just type C sharp in the consul. 20 00:01:18,780 --> 00:01:22,730 To demonstrate how to create an array let's create an array of strings. 21 00:01:22,730 --> 00:01:27,230 To do this, we first specify that the type of objects we want to store in the array. 22 00:01:27,230 --> 00:01:28,530 In our case, it's string. 23 00:01:29,850 --> 00:01:32,130 To make this an array of strings we had opening and 24 00:01:32,130 --> 00:01:34,180 closing square brackets here after the typing. 25 00:01:35,490 --> 00:01:37,410 Then we give the array of name. 26 00:01:37,410 --> 00:01:39,710 I'll call it favoriteThings. 27 00:01:39,710 --> 00:01:43,340 The square brackets here signifies that this is an array. 28 00:01:43,340 --> 00:01:47,990 We just declared a string or a variable, but we haven't put anything inside it yet. 29 00:01:47,990 --> 00:01:50,640 In fact it isn't even an array yet 30 00:01:50,640 --> 00:01:54,800 to see why type the variable name favoriteThings in the C Sharp repo. 31 00:01:58,470 --> 00:02:00,180 It printed out null. 32 00:02:00,180 --> 00:02:02,730 Null has a special meaning in C sharp. 33 00:02:02,730 --> 00:02:05,310 It represents the absence of a value. 34 00:02:05,310 --> 00:02:08,920 For most types, when we declare a variable without assigning anything, 35 00:02:08,920 --> 00:02:10,940 it's set to null by default. 36 00:02:10,940 --> 00:02:14,010 The exception to this rule is numeric types and struct types. 37 00:02:15,240 --> 00:02:20,440 Numeric types such as int and double have a default value of zero. 38 00:02:20,440 --> 00:02:23,440 We'll learn about struct types in future courses. 39 00:02:23,440 --> 00:02:26,330 A string array is just another type of class. 40 00:02:26,330 --> 00:02:29,980 In order to create one we need to instantiate it. 41 00:02:29,980 --> 00:02:34,770 We do this similar to the way we instantiate any other type, by using new. 42 00:02:34,770 --> 00:02:38,040 Only, we don't need to provide parentheses at the end. 43 00:02:38,040 --> 00:02:41,060 We do need to specify how long the array should be. 44 00:02:41,060 --> 00:02:45,040 We put how many items arrays can hold right here between the square brackets. 45 00:02:46,050 --> 00:02:48,210 I'll make it three items long. 46 00:02:48,210 --> 00:02:50,980 We have an array that can hold three strings. 47 00:02:50,980 --> 00:02:54,050 To see what we did, type favorite things in the console again. 48 00:02:55,630 --> 00:02:58,540 What we're seeing is the contents of the array. 49 00:02:58,540 --> 00:03:00,470 As you can see, everything is null. 50 00:03:00,470 --> 00:03:03,480 But now there are three nulls, one for each item in the array. 51 00:03:04,690 --> 00:03:08,610 Their null because we haven't said what we want our three things to be it. 52 00:03:09,750 --> 00:03:12,050 There are a couple ways we can do that. 53 00:03:12,050 --> 00:03:14,920 First, we can set individual items. 54 00:03:14,920 --> 00:03:18,290 Let's set the first item in the array to sunshine. 55 00:03:18,290 --> 00:03:21,694 We do that by first saying which index in the array we want to change and 56 00:03:21,694 --> 00:03:22,877 then assign it a value. 57 00:03:28,312 --> 00:03:32,640 Notice that I type the index, 0, right here in between the square brackets. 58 00:03:33,700 --> 00:03:35,020 Let's take a look at the array now. 59 00:03:37,120 --> 00:03:40,570 Now the first item in the array has been changed to sunshine. 60 00:03:41,940 --> 00:03:45,510 We can get the value from a specific location in the array like so. 61 00:03:48,650 --> 00:03:52,381 By using index zero, we're getting the first item in the array. 62 00:03:52,381 --> 00:03:56,624 Sometimes we want to set all of the items in the array at the same time that we 63 00:03:56,624 --> 00:03:57,385 declare it. 64 00:03:57,385 --> 00:04:02,980 We can do that by using curly braces at the end here, with commas between them. 65 00:04:02,980 --> 00:04:10,190 So here where we're declaring the array, I'll type sunshine, 66 00:04:10,190 --> 00:04:15,702 presents, and babies. 67 00:04:17,080 --> 00:04:18,520 Let's see what our array looks like now. 68 00:04:19,870 --> 00:04:23,440 Now we have three items in the array and none of them are null. 69 00:04:23,440 --> 00:04:27,490 There's an easier way to do this because we're providing the list of items 70 00:04:27,490 --> 00:04:29,210 here when the arrays created. 71 00:04:29,210 --> 00:04:33,320 The compiler already knows how many items we want in it. 72 00:04:33,320 --> 00:04:36,040 So we don't need to type three again here. 73 00:04:36,040 --> 00:04:39,310 In fact, we can make this even shorter. 74 00:04:39,310 --> 00:04:43,400 All three of our items are strings, and the compiler knows this. 75 00:04:43,400 --> 00:04:45,330 So we don't need to specify the type here. 76 00:04:47,350 --> 00:04:52,600 Arrays are so common that Csharp has an even shorter syntax for initializing them. 77 00:04:53,790 --> 00:04:56,830 We can delete the new keyword and the square brackets and 78 00:04:56,830 --> 00:04:59,350 just list the items inside the curly braces like so. 79 00:05:00,410 --> 00:05:04,580 Finally, we often want to know how long an array is. 80 00:05:04,580 --> 00:05:10,684 For that, we can type favoriteThings.Length. 81 00:05:12,980 --> 00:05:16,060 This will always give us the full length of the array 82 00:05:16,060 --> 00:05:18,990 that includes any null items too. 83 00:05:18,990 --> 00:05:22,090 Now that we know a bit about arrays let's put them to work for us.