1 00:00:01,600 --> 00:00:06,910 ‫And this lesson you will learn about generic and non generic collections, which is one of the most 2 00:00:06,910 --> 00:00:08,500 ‫important topics in C-sharp. 3 00:00:08,920 --> 00:00:14,350 ‫So first of all, we need to understand what collections are like arrays. 4 00:00:14,350 --> 00:00:20,680 ‫Collections are classes that we can use to store many objects at the same time, or a collection of 5 00:00:20,680 --> 00:00:23,050 ‫objects, hence the name collection. 6 00:00:23,470 --> 00:00:27,520 ‫Unlike arrays, collections are not limited to one type of object. 7 00:00:27,520 --> 00:00:33,190 ‫So instead in a collection, we can store as many types of objects as we want. 8 00:00:33,880 --> 00:00:37,840 ‫Also, collections are not fixed in size like a race, for example. 9 00:00:37,990 --> 00:00:44,620 ‫By their length we can add elements to them as we go and the collection will adjust its size. 10 00:00:44,650 --> 00:00:50,440 ‫In this case, it's not called length but count, and we're going to see how to use that. 11 00:00:50,830 --> 00:00:52,600 ‫So why do we need collections? 12 00:00:52,630 --> 00:00:58,780 ‫Well, let's imagine this scenario where we are building an attendance system for a school and we want 13 00:00:58,780 --> 00:01:02,170 ‫to add students to our system dynamically over time. 14 00:01:02,350 --> 00:01:09,310 ‫Once we define our student class and start creating objects of the type student, we will face an issue. 15 00:01:09,460 --> 00:01:15,650 ‫How are we going to store them while storing them in an array is a bad idea for a few reasons. 16 00:01:15,670 --> 00:01:21,490 ‫First of all, arrays are fixed in size, so if we create an array of 500 students, then we can't have 17 00:01:21,490 --> 00:01:22,510 ‫any more than that. 18 00:01:22,540 --> 00:01:29,050 ‫Secondly, what if we decide to add other types of objects to our system like teachers, employees and 19 00:01:29,050 --> 00:01:29,740 ‫so forth? 20 00:01:30,010 --> 00:01:38,140 ‫Well, since an array is fixed in size and can only accept one type, we can use collections to solve 21 00:01:38,140 --> 00:01:38,950 ‫the issues. 22 00:01:39,250 --> 00:01:45,610 ‫So we use them to store, manage and manipulate groups of objects more efficiently. 23 00:01:46,720 --> 00:01:50,680 ‫So collections can be used to do a lot of operations in a group. 24 00:01:50,680 --> 00:01:59,260 ‫For example, adding, deleting, replacing objects, searching for particular objects and even copying 25 00:01:59,260 --> 00:02:00,070 ‫objects. 26 00:02:00,640 --> 00:02:06,640 ‫So now there are two different types of collections, non generic collections which we can use to store 27 00:02:06,640 --> 00:02:14,040 ‫any type of objects and they are inside of system collections inside of that namespace. 28 00:02:14,050 --> 00:02:19,270 ‫And then we have the generic collections which is limited to one type of object. 29 00:02:19,270 --> 00:02:24,460 ‫So we use them if we are sure that we don't need different types of objects that we want to store. 30 00:02:24,460 --> 00:02:28,900 ‫And they are located in system dot collections dot generic. 31 00:02:28,900 --> 00:02:34,270 ‫So in that namespace, so non generic collections, let's look at some examples. 32 00:02:34,270 --> 00:02:42,790 ‫So here we have this int num equal one is five, then we have this float num two is 315 string name 33 00:02:42,790 --> 00:02:49,330 ‫is for example Dennis and then let's create this new array list, my array list and we add num one to 34 00:02:49,330 --> 00:02:55,450 ‫it, then we can add num two to it and we can add a name to it. 35 00:02:55,450 --> 00:03:02,080 ‫So you see here that they are non generic, which means it allows us to use different types. 36 00:03:02,080 --> 00:03:05,110 ‫So one type was an integer, the other type was a float. 37 00:03:05,110 --> 00:03:09,340 ‫So number two was a float and the name was a string. 38 00:03:09,340 --> 00:03:13,540 ‫So we can add all of those to our array list. 39 00:03:13,840 --> 00:03:15,250 ‫They are of different type. 40 00:03:18,070 --> 00:03:23,020 ‫So you see, we're using the ADD method in order to add them to our list. 41 00:03:23,050 --> 00:03:30,550 ‫Now we could use the methods that we were talking about before, which was copy, delete, replace and 42 00:03:30,550 --> 00:03:30,980 ‫so forth. 43 00:03:31,000 --> 00:03:36,730 ‫We could use them in here or with this array list because an array list is a non generic collection. 44 00:03:37,660 --> 00:03:44,830 ‫And now we can go through all of the elements in my array list using the for each method and because 45 00:03:44,830 --> 00:03:46,810 ‫it's not of one particular type. 46 00:03:46,810 --> 00:03:50,650 ‫So the elements inside of our array list are not of one particular type. 47 00:03:50,710 --> 00:03:58,510 ‫We need to use object element in my array list we cannot use int because there are floats and strings 48 00:03:58,510 --> 00:03:59,320 ‫in there as well. 49 00:03:59,320 --> 00:04:01,810 ‫We cannot use float because there are innocent strings as well. 50 00:04:01,810 --> 00:04:08,500 ‫So that's why we are using object which is very generally used type that allows all kinds of types. 51 00:04:08,530 --> 00:04:13,060 ‫The object type is basically incorporating all other types. 52 00:04:19,620 --> 00:04:22,560 ‫So now let's look at the generic collection. 53 00:04:22,770 --> 00:04:29,520 ‫So this one will have animal one, animal two, animal three, and it will be off type list. 54 00:04:29,520 --> 00:04:31,800 ‫And we're going to call it my list. 55 00:04:31,800 --> 00:04:35,070 ‫So you can see this list can only store strings. 56 00:04:35,070 --> 00:04:37,800 ‫So we're creating this strings list. 57 00:04:37,920 --> 00:04:43,650 ‫And since the list is a generic collection, it can really only hold one type of object. 58 00:04:43,680 --> 00:04:47,250 ‫So we have to specify that type when defining that list. 59 00:04:47,250 --> 00:04:50,010 ‫So here we are specifying its of type string. 60 00:04:50,010 --> 00:04:56,880 ‫So the data that we can pass to it or save in it or store in it will only be strings by giving the type 61 00:04:56,880 --> 00:04:58,710 ‫between an angle brackets. 62 00:04:58,710 --> 00:05:02,280 ‫In this case here you can see angle brackets string like this. 63 00:05:02,280 --> 00:05:09,300 ‫So we technically would say that my list is a collection of type list, string and upcoming videos of 64 00:05:09,300 --> 00:05:09,870 ‫this chapter. 65 00:05:09,870 --> 00:05:14,880 ‫We will explore different types of generic and non generic collections, and we will discuss the benefits 66 00:05:14,880 --> 00:05:18,600 ‫of each one of them and how and when to use them. 67 00:05:19,020 --> 00:05:22,620 ‫So this was just a very quick introduction to the two topics. 68 00:05:22,620 --> 00:05:27,270 ‫We're going to see a bunch of examples and then it will become a lot more clear. 69 00:05:27,900 --> 00:05:28,320 ‫All right. 70 00:05:28,320 --> 00:05:30,420 ‫So see you in the next video.