‫And this lesson you will learn about generic and non generic collections, which is one of the most
‫important topics in C-sharp.
‫So first of all, we need to understand what collections are like arrays.
‫Collections are classes that we can use to store many objects at the same time, or a collection of
‫objects, hence the name collection.
‫Unlike arrays, collections are not limited to one type of object.
‫So instead in a collection, we can store as many types of objects as we want.
‫Also, collections are not fixed in size like a race, for example.
‫By their length we can add elements to them as we go and the collection will adjust its size.
‫In this case, it's not called length but count, and we're going to see how to use that.
‫So why do we need collections?
‫Well, let's imagine this scenario where we are building an attendance system for a school and we want
‫to add students to our system dynamically over time.
‫Once we define our student class and start creating objects of the type student, we will face an issue.
‫How are we going to store them while storing them in an array is a bad idea for a few reasons.
‫First of all, arrays are fixed in size, so if we create an array of 500 students, then we can't have
‫any more than that.
‫Secondly, what if we decide to add other types of objects to our system like teachers, employees and
‫so forth?
‫Well, since an array is fixed in size and can only accept one type, we can use collections to solve
‫the issues.
‫So we use them to store, manage and manipulate groups of objects more efficiently.
‫So collections can be used to do a lot of operations in a group.
‫For example, adding, deleting, replacing objects, searching for particular objects and even copying
‫objects.
‫So now there are two different types of collections, non generic collections which we can use to store
‫any type of objects and they are inside of system collections inside of that namespace.
‫And then we have the generic collections which is limited to one type of object.
‫So we use them if we are sure that we don't need different types of objects that we want to store.
‫And they are located in system dot collections dot generic.
‫So in that namespace, so non generic collections, let's look at some examples.
‫So here we have this int num equal one is five, then we have this float num two is 315 string name
‫is for example Dennis and then let's create this new array list, my array list and we add num one to
‫it, then we can add num two to it and we can add a name to it.
‫So you see here that they are non generic, which means it allows us to use different types.
‫So one type was an integer, the other type was a float.
‫So number two was a float and the name was a string.
‫So we can add all of those to our array list.
‫They are of different type.
‫So you see, we're using the ADD method in order to add them to our list.
‫Now we could use the methods that we were talking about before, which was copy, delete, replace and
‫so forth.
‫We could use them in here or with this array list because an array list is a non generic collection.
‫And now we can go through all of the elements in my array list using the for each method and because
‫it's not of one particular type.
‫So the elements inside of our array list are not of one particular type.
‫We need to use object element in my array list we cannot use int because there are floats and strings
‫in there as well.
‫We cannot use float because there are innocent strings as well.
‫So that's why we are using object which is very generally used type that allows all kinds of types.
‫The object type is basically incorporating all other types.
‫So now let's look at the generic collection.
‫So this one will have animal one, animal two, animal three, and it will be off type list.
‫And we're going to call it my list.
‫So you can see this list can only store strings.
‫So we're creating this strings list.
‫And since the list is a generic collection, it can really only hold one type of object.
‫So we have to specify that type when defining that list.
‫So here we are specifying its of type string.
‫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
‫between an angle brackets.
‫In this case here you can see angle brackets string like this.
‫So we technically would say that my list is a collection of type list, string and upcoming videos of
‫this chapter.
‫We will explore different types of generic and non generic collections, and we will discuss the benefits
‫of each one of them and how and when to use them.
‫So this was just a very quick introduction to the two topics.
‫We're going to see a bunch of examples and then it will become a lot more clear.
‫All right.
‫So see you in the next video.