C# for total noobs - Part 18: Collections and List

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there and welcome back to a new lesson of the c sharp for total noobs training this one will go one step further once again and we'll talk about a very interesting topic that we'll probably cover during the next couple of parts and or sessions and this is the topic of collections and this topic is kind of like a little bit related with erased actually all the lessons that we had recently are related to arrays in some way because first we talked about race and about multiple multi-dimensional arrays and then about strings which are nothing else than an array of characters or chars so right now we go one step further and we talk about something that uh kind of like have a lot of the concepts that we have encountered for the arrays but that are more flexible and these are collections and there are different types of collections we will actually discuss them one by one the most common that we will probably use in virtually any application that you build is the list and then here you see on this title is list of t will go into what is t actually means and we'll see also of course in practice but before we get on going with the coding part let's have a very very brief understanding about what collections are so when we talked about arrays we told or we said that they are actually a way of grouping objects of the same type now collections are actually the same thing so the collections provide us a way to group objects of the same type i would add although this is not really clear here and i will briefly also say or in in a few moments i will also explain why but collections provide a more flexible ways uh to work with group of objects because it actually or collections usually allow us to dynamically grow and shrink the group of objects so what does this actually means we said that when we talked about arrays one very important thing about the array is that it has a fixed size you cannot simply add elements to to an array that actually are over of the capacity of the array that you have declared at the beginning and you cannot of course also remove elements from the array but with collections things are totally different in collections we don't have to provide a fixed size for them so they can grow dynamically if we add if we add something then the collection will get bigger if we remove something then the collection will actually shrink so it's very very dynamic for us to work with and there are some collections that are a little bit more specific where we can even uh store group of objects let's say in form of key and value pairs and we will see some scenarios in which this type of collections or grouping objects based on keys could prove to be very very important now i said that usually collections are also or contain elements or objects of the same type now in collections things can be also a little bit different because theoretically you could have collections that are not strongly typed so you can have a list in which you don't specify exactly what type of objects will contained at least but to be honest this is not something that we currently do when we write code so whenever we work with collections in csharp and net we always use all of let's say the so-called generic collections which which also provide an exact type for the objects that will be part of that collection now as we saw in the first videos when we talked about visual studio and the structure of of our projects now we have also talked about namespaces at a certain point and there is a namespace which is called system collections generic that actually contain all the collections that that we that we can use in c sharpen.net but only the generic version of them so the version in which you can specify exactly what type of objects you want to add in that collection so the advantage of of this is of course then that whenever you retrieve an element from a generic collection do not have to to determine its data type or or convert it to a different data type because you have a very high predictability you know that whenever you retrieve an element from a collection it would be always of the declared type now when we go to the coding part we'll exactly look into also uh how how or what actually this this idea of generic means now before we go into the coding part and enter the first type of collection that we will walk through and that will be the list i just want to briefly show you four type of collections four types of collections of course in c-sharp there are several other types but these are the types that two would uh actually encounter more often than the others and we have the dictionary of t key and t value we'll look into exactly what this means when we'll talk about the dictionary and this is the type of the collection in which i said that we can store objects but we can actually store them as key and value pairs so for each object we would have also an appropriate key and of course the keys need to be unique but we look into that deeper when we talk about dictionaries then we have the really most common collection type that you will use on a day-by-day basis so you will i guess every day if you start working as developer there will be no day in which you want to work with list so the list is a very simple representation of a group of objects that can be of course accessed by by index and it provides us methods or behavior or functionality to uh to sort search and modify uh the list so this is one other very nice thing about lists and collections in general that they usually also provide methods for sorting for ordering and things like that which on arrays actually don't happen by default okay then we have cues and stacks which are two very common data types in programming so the concept of q and the concept of stack are virtually exactly the same in all programming languages and even though you won't use queues and stack that often uh in your day-to-day code writing these are i said very important data types and as a developer even if you are at the early stages of your creative you have to be aware of what the queue is what are the characteristics of a queue what the stack is what are the characteristics of a stack and so on and then there is also the sorted list of t key and t value which represents also something like a dictionary and we'll see exactly what the what a dictionary is when we'll talk about the dictionary but the only thing here is that this list is actually sorted and this is very important because in this case you can know the exact order basically of the elements that are part of that specific sorted list of t key entity value and yeah that's actually mostly it now before we go to the code i just want to go here let let's look at the list because this is the first collection that we will actually look into and we have here this idea of list of t and you see here this t in angle brackets so what is this t whenever you see something like uh a t in angle brackets or it could be also other type of notations like t key and t value as as we see for the dictionary but whenever you see this type of notation in c sharp it means that we talk about a generic class what does this actually mean it means that we have or when we define the class in case of the list we don't have to because microsoft has already written that this class for us but we need to know how to consume it but the idea is that when you create or when you define the class you define also a placeholder for a data type and this t is nothing else than a placeholder for a data type that you would have to provide once you declare and or instantiate the list so this means that whenever we want to create a list we will have to say list of int or list of string or a list of double or list of my class and and things like that so whenever we want to create a new list we have to also declare in angle brackets the data type but this is what we need to do as developers when we consume when we use this class list if we would write our own generic class and we want to make it generic then we would have also when we write the class when we defined the name of the class to also specify in angle brackets the t so it would be my class of t this is how we read it once again this t is actually a placeholder for a data type that we have to provide when we declare and instantiate that certain list in this case or generally speaking when we declare and instantiate an object of a generic class but about generic classes we'll talk a little bit more at a certain point for now we just want to focus on this concept of collections and in this case the concept of list so let's go over to visual studio right now and start actually working with a list but in this case we'll do this a little bit different than we usually did the demos and we'll create here in the same project a class for each type of collection demo that we want to go through so i say i want a class and i will call this class list demo because we want to demonstrate this um let's make this class public and in this class there's actually nothing very fancy we'll just provide a static method public static it could also be void and here run demo okay and here is actually all the code that we will write so for in in other lessons we have written everything here in in static void main and we executed the program uh let me just also make this uh runnable but in this case we just uh have this list demo with this um with this method and we'll put all the console right lines and in the code that we want to run here and instead in this program.cs file we'll just say here list demo dot rundown and that's it good now of course when this happens everything that we place here will be executed and this means that all our console write lines and read lines and things like that will get executed so we will be able to see outputs on the console and also if necessary read inputs from from the console now let's get started and working with lists and here the first part is uh well uh let's see how we can declare and uh instantiate lists so this is the first thing that we want to look into and there is there are different ways that we can do this so the first way that we look into is something that i guess we have already used for for regular objects and in this case they are called collection initializers what does this actually mean it means that when we declare the collection and we instantiate it we already provide also the elements in that collection and this would be for instance let's create a list of string and here just remember what we uh said just a little bit earlier when we want to use this list as we want to use the generic list which is the list of t so when they have created list let's go let's press f12 we see that it is list of t so when microsoft has written this class it has declared this t which means that it is a placeholder so it means that whenever we want to create a list we we need to provide here a data type that will define the elements or the type of the elements that will be contained in this list and this is called generics so let's call this cities and let's add here a list of cities in this case we instantiate a list just like a regular class because list and all the collections are regular classes and what is the collection initializers as said connect we can use a collection in initializer when we know exactly the data that we want to put into a collection right when we started so we can just put these brackets here this uh this curly braces and here for instance we can provide let's provide cities so um one of my favorite cities seattle then one of my other favorite cities is munich and one other city that i really enjoy very much is lisbon although i visited lisbon only once but i really i really enjoyed it and of course we need a semicolon here so this whole part the fact that we use these curly brackets right after we have created a new instance like with new list of string and that we have provided the values this actually means that we have used a collection initializer now for instance there is a second way to do this if we want for instance to defer the adding of elements for a later time we can simply use just a simple object creation because at least in the end from a perspective of well what classes are is nothing else than a class so we can have here another list of string and let's call it names this time and we just say it's new list of string and it would beat but now we have created the object so after this line executes we have already a functioning object it will be of course empty because there is no element in the list in comparison to the first uh way that we did it so with with the collection initializer in this case the list is already populated with three elements in this case we just create a list but the list will be empty now what we can do is let's for instance add elements or add new elements in the list here um for instance let's also save adding elements to the list of t okay so what we can do right now for instance and here is where the big difference comes when uh when we compare these two arrays collections and this means lists also are dynamic so you are really not or you don't have to obey or follow the consequences of a fixed size enumeration of objects in this case the size is dynamic so you can at whatever time you want add a new element to the list and once again at whatever time you want you can remove elements from the list so it's just simple or as simple as that in code this is once again very very simple when we click on names we have here the add method so of course add method if we hover the mouse over it adds an object um to the end of the list of t so in this case uh names is a list of string which means that we would have to provide a string and uh well let's provide here for instance my name but to see why we actually use generics is that we have we said here that we want the list to be of data type string so this means that all the elements in this list need to be a string if i for instance try to add a number we get an error here and here well the error is not exactly or maybe it's not very intuitive at least if you are at the beginning but it says that our argument one cannot convert from type into string what this actually means is that this method right now x expects that our argument should be a string and i have provided an integer here so this is why we cannot even compile this code right now so this is the power of having a type save a programming language and using generics and be always sure about the data types of the elements in a collection so let's get back and add a string now for the cities for instance we can uh though also cities but add so once again it's no matter that we have already added here three values at whatever time we want we can come back and say hey i want to add something else here in this case i want to add mishwara which is actually the city that i am currently living in now okay the idea here is very very simple this is how we can add different elements and now we because there are some some very or there are there is a lot of code that needs to be written here i will just copy paste and then go through what each line of code actually does because it doesn't really make sense to really type everything it will take a long time and it's it's not justifiable but if you're watching this this training or this session then please feel free to pause this video and type and write everything by your hand as i said it is very important and i cannot emphasize enough how important it is that you write code uh by your own hand because this is how you get actually accustomed about syntax and code writing and how visual studio behaves and even shortcuts and things like that so this is once again very very very important just use or stop the video type everything and then go on with the video so now let's go back here and just talk about what we try to achieve here because we are doing a lot of console.writelines that wants to underline the different functionalities that we have on a list and that we don't have for instance on an array now on collections when it comes to counting the elements because we also can do this when it comes to collections and of course when we talk about the list in this case the property is not called length anymore but it's called count so here what we do here is we we just count uh how many cities do we have right now in this list then a collection as said as it is a group of objects and at least it's if from this perspective it is similar to the array but this also means that you can access each element also by its index so in this case we say that the second element in the list is and we use here cities and provide here one because as the array also collections or indexes of collections are also zero based so they start from zero so this means that the last index of a collection will always be the collection count minus one exactly like it was the case for the array so this means that when we try to access the element on the position one or on index one we try to access the second element which in this case should be this one so this is one way that you can access uh one one element of the list but then you can or you have also the possibility to remove an element from the list in this case we just want to remove an element from the list by uh specifying the index on which we want to remove the element so in this case we want to remove the city on the index one so it would be also this second city in the collection and uh then we will see and we will or we write this same line again and we will see that in this case after we have removed an element at position one everything has actually shifted uh one index back so it means that in this case on index one we will have lisbon because munich has gone away and the least one will be on the second place and this means it will be on the first uh on the index value one uh now of course we can also insert at a certain index and here would mean that we would insert on the index one uh the same city here that we added previously in in collections we can have also elements that are let's say fairly equal i would say and then uh the index of the city and we see the cities the cities this is the list and we have here the index off and we want to get the index of this city right now and then what other thing that we do and that we cannot directly do on arrays we can sort so we have on list on on the class list of t we have here a sort method that's kind of will sort our elements based on how sorting should be done for this specific data type so if we want to sort uh strings it will be alphabetically in ascending order if we want to store integers then uh it would be in in ascending order once again and uh things like that so this this will the source do now for arrays we would have to write our own methods to sort an array and then return the sorted array but for lists we can simply just use what we already have in the language so in this case we go then with the for each loop through each city in the cities list and we of course write the city name and then we have another method on the list which is reverse which actually will reverse everything so it will reverse all the elements in the list and then what we do is we loop once again to the list and then we print once again all the cities and we'll see that those are in reversed order so let's maybe put here also a breakpoint and let's run this application and go through all these lines one by one and then follow exactly what happens in the console and try to explain why that actually happens so first of all of course we hit our breakpoint which is called and for now we don't have anything in the console to look at so let's run or let's go one step further so once again here we just want to say how many elements do we have in in this list and we use the count property for that if we hover the mouse on it while we are debugging and we are uh we are actually in this breakpoint we can also see that for here it indicates that we have four elements in the list because three were added while initializing the list with a collection initializer but another one was added a little bit later here so in this case when we execute this line it should print in the console the number four in this case current number of cities in the list is four which is correct and expected now what we do here when we execute this other line so here once again we want to find out uh which city or what value actually is on the index one and this is how we do this we can use an indexer just like we did for the arrays to access the value on index 1. so let's execute it and i would expect it to be munich so i'm not sure exactly what uh happened here because the execution of uh of this program just uh my visual studio is not responding anymore okay because i because i have selected something in the console and it was blocked for writing so but if we open it uh once again right now we see that the second city in the list is munich which is correct but then we say cities remove ad so what this means is once again is hey collection or hey list here please remove the element that resides on index one now we know from the previous line that on index one we have the city munich so in this case when we execute this line of code this city should be removed and while debugging if we hover the mouse over the list we can see that right now the list has only three elements and if we go in it we see that it's only seattle is born and timiphara so there is no munich anymore it means it was removed now to make sure that that this really happened we also print this in the console and here we see that in this case the second city in the list right now is lisbon and not munich anymore because munich was removed and all the others were actually shifted one index position back so basically when you remove elements from the list there won't be any blank spaces so everything will be shifted so that we have a continuous enumeration of elements and so that on each index we also have a value now here once again we insert a new city and usually when we insert this happens actually at the end of the list and once oh sorry here we we say that we want to insert the city at position one or at the index one so what this means is that at position one so between here city and uh seattle and lisbon we will insert also the value to myshara this means that all the others will be shifted one index value forward so if we place our mouse over cities we see that this already happened so we have here seattle then tim sheridan lisbon and then once again a timisoara which is a very very okay so this really happened we also uh write it in the console and uh yeah this is uh the index of temperature is one which is correct and now we have this other method which is sort now uh here in this sort and let's uh let's put a breakpoint here because it really um it it really doesn't make sense to uh to go through each uh iteration of this loop but we can see that after the sort if we hover the mouse on this we see that right now this is sorted alphabetically so the first one is lisbon then it is seattle and then the two chemistrial values which are should also be equal so if we just hit continue in this case uh we will see that this was also printed in the console so this one settled to me foreign which is correct and then what we do we do exactly the same thing we just reverse so in this case let's also put a breakpoint here on this line so we reversed the list so if we go to this line of code and if we hover the mouse on cities then we see that it is right now reverse because right now we start with me foreign in seattle and then lisbon and then of course we have here this uh this forage that will print everything uh on in the console so right now if we look in the console we can see that uh yeah we uh okay so i guess we have to put a breakpoint sorry uh one step further and continue once again and right now when we look in the console once again we have printed the list and if we compare these two entries we see that they are completely reversed so exactly what um what we should expect now uh let's close this application for now now we don't need this breakpoints before we we stop i i want to show you also something else because when it comes to insert here um there are actually different ways to add to add the elements of the list and we have actually looked into two and whenever you want to add an element and you are not worried or you don't really care on what index that element should be added then we have the add method and that would simply add that element at the end of the list now if you want to insert an element at a certain position then you would have to use this insert and here you have to specify the position so on which index you want to insert and then to specify the value or the object that you actually want to to insert so once again just remember if you want to add elements to a list you have these two options if you don't care about the index then you can simply use add and the element will be added at the end of the list and then you have here cities insert if you want to to insert at a specific um at a specific index now for removing we have or we saw here that we have this remove at index this is when we wanted to remove an element that is on a certain index but if we once again don't know exactly the index of the element that we want to remove but we know the element that we want to remove in this case we have two times this element which is called mishwara so let's just remove it so in this case we can just simply say cities dot remove and here we can just simply say that we want to remove this this one and uh yeah let's run this application again and when we print all the cities in reverse order then timothy should come up only once and not twice as it was before so indeed one of the entries was removed now what actually happens here when we have this remove and we specify the value if we have two elements with the same value like like it was the case here right now in our scenario it will actually remove the first element that it finds so it really will remove the element that we added first here so this one which would have been i guess on index one because it was reversed right now so once again the remove removes all or removes the first occur or the the first occurrence of the element that we specify here now as we as we have probably observed there is also a remove add sorry the remove all which will actually clear the entire list so if we uh what's the problem here uh no not remove it sorry let me just go here once again cities dot remove all okay and then we have here to specify uh okay we can specify here a predicate that's a little bit more complicated we will talk about actions and what in delegates and what could be a predicate but here we have we can um remove actually all and we have we can specify a condition and well in this uh case we don't know exactly what what condition to specify but the idea is that uh well we have also an option to remove all the elements that uh that match a certain criteria and if we want to clear the list totally uh then we have a method which is called clear and the clear method will clear the entire list so it will remove all the elements and we will end up with an empty list just like we had here with names this is what clear does once again uh remove and we specify an element that we want to remove we have then remove add where we specify an index and we remove the element on the specific index we have then remove all and we can specify a condition and then all elements that that meet the specific condition will be removed and then we have clear which will actually clear the entire list so no element will remain in that specific list so in this case if we run the application now we will see that uh basically the last line will not print anything because here the list is empty so we don't have any elements in the list anymore and yeah so just remember these different ways of adding uh and uh yeah then we there's different way on removing then we can also clear and then we have the different functionalities that are very specific to collections like the count and like the fact that we can access elements by their index and find out the index of a certain element and uh things like that and that pretty much sums it up so lists are really not very complicated to understand because it's also something that we or that is very natural and easy for us to actually visualize because it's actually a list and we all do all kinds of lists all the time so in this case uh i guess that that would make it and uh yeah that's it for for the concept of list so uh yeah thank you very much for for watching this lesson and uh i can't wait for the next one and i hope to see you there but until then i really wish you the best and uh good luck
Info
Channel: Codewrinkles
Views: 45
Rating: 5 out of 5
Keywords: C#, CSharp, C# for total noobs, collections in C#, C# collections, C# List, C# training, C# for beginners, programming, learn to code, .NET, DotNet
Id: ZuARPrKqEx4
Channel Id: undefined
Length: 35min 20sec (2120 seconds)
Published: Sat Oct 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.