1 00:00:07,230 --> 00:00:07,670 Hello. 2 00:00:08,130 --> 00:00:11,940 In this lecture, we're going to talk about this topic 25. 3 00:00:12,270 --> 00:00:14,340 What is released? 4 00:00:14,850 --> 00:00:20,220 Let me take you on a journey back in time, a long, long time ago. 5 00:00:20,310 --> 00:00:24,780 So before 2006, Dot Net was at version one. 6 00:00:25,140 --> 00:00:30,930 It was still a pretty new framework because its initial release was in 2002. 7 00:00:31,260 --> 00:00:37,410 The C-sharp language itself did not look much as it does now at this version of Dot Net. 8 00:00:37,650 --> 00:00:40,050 There was no such thing as generics. 9 00:00:40,440 --> 00:00:46,950 If you wanted to have a collection of numbers and a collection of strings, arrays were your best choice. 10 00:00:47,220 --> 00:00:53,430 You couldn't count on things like the generic list of teams that can hold any type of items. 11 00:00:53,910 --> 00:00:57,060 But as we learned in the lecture, What is an array? 12 00:00:57,420 --> 00:00:59,610 Our can be pretty awkward. 13 00:01:00,000 --> 00:01:01,350 They have fixed size. 14 00:01:01,590 --> 00:01:06,150 They also don't provide any convenient methods like add or remove. 15 00:01:06,540 --> 00:01:13,830 In other words, with arrays only, creating a complete, efficient and maintainable application that 16 00:01:13,830 --> 00:01:17,700 made some real business needs could have been a pain in the neck. 17 00:01:18,060 --> 00:01:22,110 Luckily, there was another way than using plain arrays. 18 00:01:22,410 --> 00:01:30,870 The ArrayList type on our list is a dynamic collection, so a collection we can resize that can hold 19 00:01:30,870 --> 00:01:32,370 any type of items. 20 00:01:32,820 --> 00:01:41,940 And just to be clear, at the same time, single released can hold its strings, dates, times and anything 21 00:01:41,940 --> 00:01:44,040 we want at the same moment. 22 00:01:56,000 --> 00:02:03,110 In statically typed languages like C Sharp, this is at least weird, but how does it work? 23 00:02:03,450 --> 00:02:10,160 Well, the released simply treats everything it holds as instances of system object type. 24 00:02:14,830 --> 00:02:19,480 Here we can see that the element of index zero is simply an object. 25 00:02:19,720 --> 00:02:26,470 After all, everything in C-sharp can be considered an object because every type is derived from the 26 00:02:26,470 --> 00:02:27,580 object glass. 27 00:02:28,000 --> 00:02:29,770 But there is a problem. 28 00:02:30,160 --> 00:02:34,700 If the item we want to store in an array list is of a value type. 29 00:02:34,720 --> 00:02:39,220 It must be boxed to be treated as object, which is a reference type. 30 00:02:39,520 --> 00:02:42,130 Boxing is not a cheap operation. 31 00:02:42,370 --> 00:02:48,100 It requires moving the value from the stock to the heap and creating a reference to it. 32 00:02:48,310 --> 00:02:54,160 Also, at some point, we'll need to unbox this item to access the underlying value. 33 00:02:54,520 --> 00:02:59,110 Finally, all boxed objects will need to be cleaned by the garbage collector. 34 00:02:59,470 --> 00:03:04,120 We'll talk more about the performance of the unreleased later in this lecture. 35 00:03:04,600 --> 00:03:08,050 Since are released can halt unagi of elements. 36 00:03:08,140 --> 00:03:11,710 We don't really know what they are and how can we use them? 37 00:03:12,070 --> 00:03:17,800 If I have a list of IANS, I know I can, for example, calculate the sum of them. 38 00:03:18,070 --> 00:03:24,780 If I have a list of three things I know I can concatenate them, but what can I do with the elements 39 00:03:24,970 --> 00:03:26,260 of an array list? 40 00:03:26,620 --> 00:03:31,300 The truth is our list was almost never used to like this. 41 00:03:31,600 --> 00:03:36,250 In most practical cases, it was holding elements of the same type. 42 00:03:46,890 --> 00:03:53,370 That looks almost like generic lists, which again, were not present in the alternate before version 43 00:03:53,370 --> 00:03:53,820 two. 44 00:03:54,210 --> 00:03:57,240 But those variables are very problematic. 45 00:03:57,660 --> 00:04:04,430 Let's say I wanted to create a method that calculates the sum of elements in the collection of numbers. 46 00:04:20,690 --> 00:04:27,260 This method doesn't compile each element of the array waste is an object, so I can simply add it to 47 00:04:27,260 --> 00:04:28,010 the result. 48 00:04:28,310 --> 00:04:30,140 It may not be number at all. 49 00:04:30,320 --> 00:04:34,910 It can as well be as Twig or Abu, so two added to the result. 50 00:04:35,060 --> 00:04:38,300 I must first cast it and hope it will succeed. 51 00:04:40,540 --> 00:04:44,920 Just to be sure, let us handle the invoked exception in this matter. 52 00:04:55,500 --> 00:05:02,300 As you can see, we were forced to create a lot of code that would not be needed if we knew what types 53 00:05:02,310 --> 00:05:03,960 exactly do we deal with. 54 00:05:04,230 --> 00:05:11,550 In other words, if we're given a list of items instead of an are released, so we know the first big 55 00:05:11,550 --> 00:05:15,930 disadvantage of at least we don't know what is stored inside. 56 00:05:16,110 --> 00:05:19,980 So we must be ready for a lot of casting and error handling. 57 00:05:20,250 --> 00:05:27,090 The other disadvantage is the performance that they mentioned before when storing value types are released. 58 00:05:27,180 --> 00:05:30,540 They must all be boxed, which can impact the performance. 59 00:05:30,540 --> 00:05:32,130 Very much so. 60 00:05:32,130 --> 00:05:37,560 In this case, the natural question is when to use our lists of our lists. 61 00:05:38,040 --> 00:05:46,050 Well, the answer is never, unless for some reason you must work in applications written in 2001. 62 00:05:46,200 --> 00:05:48,240 But I honestly hope that you don't. 63 00:05:48,480 --> 00:05:55,020 Even if you do consider upgrading the version of the botnet rather than working in this ancient technology, 64 00:05:55,470 --> 00:05:58,470 you might then ask Why do we learn about this? 65 00:05:58,590 --> 00:06:06,240 If it's not a big deal since 2006, first of all, the questions about our release are quite liked by 66 00:06:06,240 --> 00:06:11,760 interviewers as they can be approached while discussion about static and dynamic typing. 67 00:06:11,880 --> 00:06:15,900 What we learned more about in the lecture about the dynamic keyword. 68 00:06:16,280 --> 00:06:22,500 Secondly, as much as I hope that you don't need to work with analysts, it may turn out that you have 69 00:06:22,500 --> 00:06:25,790 to work with some legacy code that still uses them. 70 00:06:25,800 --> 00:06:29,460 And then it's important that you know what you are dealing with. 71 00:06:29,730 --> 00:06:38,190 Also, only recently, so in 2022, I've been working on a brand new application, or someone was using 72 00:06:38,190 --> 00:06:41,130 our release for reasons they couldn't explain. 73 00:06:41,610 --> 00:06:45,360 And as it turned out, it was mostly storing value types. 74 00:06:45,810 --> 00:06:52,680 Changing them to lists not only made the development process much easier as the never ending costs and 75 00:06:53,010 --> 00:06:58,380 handling could be omitted, but it also made the application over 25 percent faster. 76 00:06:58,980 --> 00:07:05,910 There is one more case when using ARE may seem tempting when you actually need to store elements of 77 00:07:05,910 --> 00:07:08,160 different types in a single collection. 78 00:07:08,550 --> 00:07:15,450 But even then, it's better to use a list of objects as it provides more functionality, then are released 79 00:07:15,450 --> 00:07:19,770 and will most likely be more consistent with the rest of the application. 80 00:07:20,400 --> 00:07:27,300 Let's summarize on our list is a collection that can store elements of any type as it considers them 81 00:07:27,300 --> 00:07:29,430 all instances of the system. 82 00:07:29,430 --> 00:07:36,810 Object type already lists were widely used in older versions of C-sharp were the generics were not yet 83 00:07:36,810 --> 00:07:37,510 available. 84 00:07:37,800 --> 00:07:43,800 Nowadays, they should not be used as their performance is impacted by the fact that they need the box 85 00:07:43,890 --> 00:07:44,790 value types. 86 00:07:45,420 --> 00:07:52,100 During the interview, you can be asked What is the difference between an array on the East and an arrow 87 00:07:52,110 --> 00:07:59,430 released on Arrow is a basic collection of fixed size that can store any declared type of elements. 88 00:07:59,880 --> 00:08:02,070 The list is a dynamic collection. 89 00:08:02,340 --> 00:08:09,000 It means its size can turn over time, and it's generic, so it can also store only declared type of 90 00:08:09,000 --> 00:08:09,780 elements. 91 00:08:10,190 --> 00:08:16,680 An unreleased is a dynamic collection that can store various types of elements at the same time as it 92 00:08:16,680 --> 00:08:20,280 treats everything it stores as instances of the system. 93 00:08:20,280 --> 00:08:29,190 Object type when to use are released over a generic list of D never unless you work with a very old 94 00:08:29,190 --> 00:08:32,190 version of C-sharp, which did not support the generics. 95 00:08:32,490 --> 00:08:38,700 But even if you do, you should rather upgrade dot net to a higher version, then work with release. 96 00:08:39,360 --> 00:08:41,580 All right, that's it in this lecture. 97 00:08:41,940 --> 00:08:45,270 Thanks for following along and see you in the next one.