1 00:00:07,080 --> 00:00:13,140 So with this lecture, we start a series about collection data structures in C-sharp. 2 00:00:13,350 --> 00:00:17,400 We'll begin with a simple but common question from interviews. 3 00:00:17,700 --> 00:00:22,740 What is a narrow array is the most basic collection easy, sharp. 4 00:00:23,130 --> 00:00:28,670 You can think of an array as a collection of boxes, each one holding a single value. 5 00:00:29,010 --> 00:00:35,670 Each box has its index starting at zero and ending at our linked minus one. 6 00:00:36,180 --> 00:00:42,900 The important thing to understand is that whilst an error has been created, its size cannot be changed. 7 00:00:43,290 --> 00:00:49,680 Because of that, errors are not the best choice if the collection that we need is going to grow or 8 00:00:49,680 --> 00:00:50,940 shrink over time. 9 00:00:51,570 --> 00:00:58,860 If we try to get or set the value at an index, that's not Indira, we'll get index out the French exception. 10 00:01:04,080 --> 00:01:08,490 This line will throw an exception because in the stern, it's not in the euro. 11 00:01:08,790 --> 00:01:12,810 The last index is for when an aura is created. 12 00:01:13,080 --> 00:01:16,470 It is filled with the default values for the given type. 13 00:01:16,620 --> 00:01:22,830 For example, an array of incense will be filled with zeros and an array of strings will be filled with 14 00:01:22,830 --> 00:01:23,460 Norse. 15 00:01:23,940 --> 00:01:30,870 We can set the values of the aura right at the moment of initialization, using the collection equalizer. 16 00:01:31,260 --> 00:01:37,970 In this case, we don't need to specify the RSI as it will be said to the count of provided elements. 17 00:01:43,250 --> 00:01:51,290 This gold will naturally create an aura of south free arrows can store and the objects of the same type, 18 00:01:51,410 --> 00:01:55,690 they can be single dimensional, multidimensional or dagat. 19 00:01:55,940 --> 00:02:00,830 For example, this is a single dimensional array that holds five inks. 20 00:02:01,310 --> 00:02:08,690 We can also define multidimensional arrays that resemble tables or mattresses that we know from mathematics. 21 00:02:13,240 --> 00:02:17,750 This is a two dimensional array that can hold up to 15 months. 22 00:02:18,160 --> 00:02:22,510 The data held in this array could be visualized like this. 23 00:02:23,290 --> 00:02:29,980 We can think of are multidimensional array as an array of arrays, which all have the same length. 24 00:02:30,370 --> 00:02:38,620 In this case, we have an array of size free and each index there is an array of size five to get or 25 00:02:38,620 --> 00:02:46,000 set an element of our multidimensional array, you must simply use two indexes instead of one separated 26 00:02:46,000 --> 00:02:46,810 by a comma. 27 00:02:50,230 --> 00:02:57,520 We can also define jagged arrows are jagged arrow is an array of arrows which don't need to be of the 28 00:02:57,520 --> 00:02:58,420 same length. 29 00:02:58,780 --> 00:03:01,660 Let's define a jagged array of integers. 30 00:03:05,750 --> 00:03:12,110 Here we defined an array of size free for which each element will be an array of ins. 31 00:03:12,500 --> 00:03:14,210 Let's set those elements. 32 00:03:22,310 --> 00:03:27,650 In this case, the structure of this Typekit arrow will look like this. 33 00:03:28,750 --> 00:03:36,340 At Index Zero, we have an array of links to add index one of them at one and at index to offload three. 34 00:03:40,330 --> 00:03:47,290 Please notice the difference in accessing the elements of a drug array in opposition to a multi-dimensional 35 00:03:47,290 --> 00:03:49,780 array in the case of drug arrow. 36 00:03:49,840 --> 00:03:54,220 We must use two sets of buckets before we continue. 37 00:03:54,250 --> 00:03:59,740 Quick information for people with a background in languages like C or C++. 38 00:04:00,100 --> 00:04:07,150 Remember that in C-sharp, an arrow is just an object and the others it's not on addressable region 39 00:04:07,150 --> 00:04:09,940 of memory, like in those of our languages. 40 00:04:10,270 --> 00:04:13,270 Arrows are reference types in C Sharp. 41 00:04:14,140 --> 00:04:14,820 All right. 42 00:04:15,100 --> 00:04:21,370 Let's see what arrows are best for and when we should rather consider using other collection types. 43 00:04:21,610 --> 00:04:26,260 First of all, arrows are the most basic and native collection type. 44 00:04:26,530 --> 00:04:32,980 They represent the data in a way that is very close to how the data is stored in the computer's memory. 45 00:04:33,400 --> 00:04:39,790 Many of our collections like, for example, lists we learn about in the next lecture use arrows as 46 00:04:39,790 --> 00:04:41,830 their underlying data structures. 47 00:04:42,310 --> 00:04:47,320 Arrows as the most native collection type have the advantage of being fast. 48 00:04:47,620 --> 00:04:56,230 We can get or set an element of the given index in a constant time so super fast in less technical terms. 49 00:04:56,440 --> 00:05:00,820 When we care about the performance, arrows are often the best choice. 50 00:05:01,270 --> 00:05:08,320 Also, multidimensional arrows are commonly used in mathematical operations as they represent matrices 51 00:05:08,320 --> 00:05:09,010 very well. 52 00:05:09,410 --> 00:05:14,740 They're also very useful whenever we need to represent a multi-dimensional structure. 53 00:05:15,070 --> 00:05:19,960 Think of our two dimensional game we styled map like chess or snake. 54 00:05:20,290 --> 00:05:26,960 The two-dimensional array of tiles would probably be the most natural and performance efficient ways 55 00:05:26,980 --> 00:05:28,930 to represent the game area. 56 00:05:29,650 --> 00:05:34,060 The disadvantage of using arrows is that their size is fixed. 57 00:05:34,270 --> 00:05:37,330 In other words, it's not a dynamic collection. 58 00:05:37,630 --> 00:05:40,630 We can't really add or remove elements from it. 59 00:05:41,320 --> 00:05:46,210 We could assume that some size of an arrow is large enough for our needs. 60 00:05:46,550 --> 00:05:53,260 Let's work on an e-commerce platform, and we want to store the items culled in the shopping cart in 61 00:05:53,260 --> 00:05:53,920 an array. 62 00:05:54,340 --> 00:05:58,120 We can declare an array of sites 100 and hope. 63 00:05:58,120 --> 00:06:02,410 No one will buy more items, but such assumptions are dangerous. 64 00:06:02,680 --> 00:06:07,300 What if someone is in a shopping rage and they really wants to buy more things? 65 00:06:07,600 --> 00:06:16,090 OK, so let's set the size to 1000, but done in 99 percent of the cases will allocate the memory for 66 00:06:16,090 --> 00:06:17,320 1000 items. 67 00:06:17,560 --> 00:06:20,680 Well, actually only two or three will be added. 68 00:06:21,100 --> 00:06:22,690 This is a huge waste. 69 00:06:23,290 --> 00:06:29,290 So in short, our eyes are great if we know the count of elements upfront, but not so much. 70 00:06:29,380 --> 00:06:36,760 If we do not during the interview, you can have questions like what is a are a drug? 71 00:06:36,880 --> 00:06:41,890 It already is an array of arrays which can all be of the different lengths. 72 00:06:42,640 --> 00:06:45,490 What are the advantages of using arrays? 73 00:06:46,030 --> 00:06:50,470 They are fast when it comes to accessing an element of the given index. 74 00:06:50,830 --> 00:06:58,000 They are basic and easy to use and great for representing simple data of size that is known upfront. 75 00:06:58,450 --> 00:07:01,960 So what are the disadvantages of using race? 76 00:07:02,380 --> 00:07:07,870 Arrows are a fixed size, which means once created, they can't be resized. 77 00:07:08,110 --> 00:07:13,990 It means they are not good for representing dynamic collections that grow or shrink over time. 78 00:07:14,260 --> 00:07:20,710 If we want to allocate the memory for all elements that may be stored, there is a chance that we allocate 79 00:07:20,710 --> 00:07:22,150 too much and waste it. 80 00:07:22,510 --> 00:07:28,570 We can also underestimate and not declare an error big enough for some add cases. 81 00:07:29,230 --> 00:07:31,150 How to resize an array? 82 00:07:31,810 --> 00:07:33,190 Well, it's not possible. 83 00:07:33,490 --> 00:07:39,070 An arrow is a collection of a fixed size and once created, it cannot be resized. 84 00:07:39,670 --> 00:07:42,110 All right, that's it about race. 85 00:07:42,640 --> 00:07:45,340 Thanks for watching and see you in the next video.