C# Generics - What they are, why they are useful, and how to create them

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
have you ever used a list of T and one of the T meant or maybe you heard the term generics but you didn't really know what that was or why is important maybe you kind of understand generics but you didn't know you create your own whatever the case this video is for you in this video I'm gonna lay the foundation of what generics are and what makes them so powerful then we'll look at a demonstration I've set up on how to keep your code dry better conform to the solid principles and make your life easier by implementing generics in a class if you're new to this channel my name is Tim quarry and it's my goal to make learning c-sharp easier this channel is full of videos explaining the various parts of c-sharp there's even a full course on building application from scratch I hope you find it useful if you do don't forget to subscribe to the channel and hit that little bell icon to get notified when I release a new video every Monday there's also a link to a mailing list in the description below that will keep you informed of all the training resources I am creating for those of you who found this content valuable I appreciate if you check out my patreon page either you become a supporter you can get exclusive behind-the-scenes access and also everyone who is a supporter at any level will get access to the goal rewards when you hit them those rewards include a tour of my office a documentary on how I make videos and even a full course the link to patreon is in the description below as well ok so let's tackle generics I just have a console application with some starter code we can diet so we can dive right into this topic before I explain everything let's start the basics the best way I say generic an action is to look at the list so I ignore everything we have here and just concentrate on this right now so this is a stag void main and I have my right line my little message here to kind of class it up a little bit so you run this right now it just goes right to the press ENTER to shut down you press Enter and shuts down that's it pretty simple stuff so let's look at the list here to see Erick's so if I say list notice it asks for the open closed angle brackets and it says it the class is system collections generic dot lists it has that T inside those angle brackets this list uses generics so if I open my angle brackets this is okay I need T and the T is the type of elements in the list so I could put let's do int another type no problem and so I can call this ages equals new list of int notice it's a list of int now list of T because int is now the type T represent the type so int is now the type that we're going to use so what does that do for us well if I say ages dot add notice it says you have to add an int item so if I try to put hello that wouldn't work because it expects to have an int in there but if I put 23 because yep that works because 23 is an int if I put 23 point 5 that will not work because that's a double not an int so what list of T does for us is that T allows us to specify which type this list is about now you may have just kind of taken us for granted if you start using c-sharp later on I mean later as in 2.0 I believe it was or later where you just kind of use this all the time and don't really think about it but it used to be we didn't have lists of TV and have generics and so instead we have ArrayList and that's in-system duck collections do not use an ArrayList I'm gonna demonstrate it just to show you what we used to have again do not use an ArrayList arraylists are deprecated we now use the generics because they're so much better not show you why let's call this just list equals new ArrayList if I say list dot add notice I haven't specified type here it says okay go ahead and add an object well we can put 23 and it says that's cool I could say list dot add hello all so cool which is the problem and so what's happening is it says well everything's an object and so I'm gonna have the list be of type object that way you can store anything you want in there if you want to specify the only int can go as list you're responsible to make sure that you only put int in this list so what happened was we had this list there's a type object and if I wanted to say int Val equals list lets me the one position zero it goes no you can't do that because we can't convert a type object to an int and say okay so it's actually an int and it says okay that works and if we run this just to kind of demonstrate it does compile and run yep no problem but what if I now said I want the one at position one which is this one right here no compile time error but at run time I get an unhandled exception it cannot cast hello to an int and see how his whole messy thing of trying to use conversions plus this stuff if you're doing conversion we were actually going to check first that's going to take some resources and so it slows your application down it's also not not really safe because you can do something like this and the only way to prevent it is to either manually scour your code and make sure you don't do anything like this in your code which is probably not your codes problem it's probably information from the user that's been the problem and so there's always issues well that's what Microsoft came in and said this stuff isn't good it's slow its runtime errors not compile time errors and it's a little dangerous and we had those boxing and unboxing things where we're actually converting them from object back to an int enter the list of T so I'm putting int here and it will not allow me at compile time meaning if I just say ages dot add hello it knows right now that's a problem that when I hit run right now that's called a compile time error we like compile time errors we don't like errors in general but if we had to choose we want a compile time error not a run time error is a run time error looks good here but it's only when you actually come across the code and come across that specific case that it will crash now you may think well yeah but it crashed right away that's only because I ran that code and ran it with a bad value what if you normally have good values but then that one user puts in a bad value well that's when you get the crash and so it's immense sporadic and trying to find out where those errors are and how they're happening is just a mess this way we know right now that that's a problem so that's what the generics are good for they they give you actual types and that's one of the things it's really important is this list of T because I specified that some int is now a list of int I can't change this down the road I can't say ages is now a list of strengths that won't work it's locked into I am a list of int so that's really helpful as well because we don't have these things looking shift over time it's not a dynamic thing where it can be changed these are strongly typed meaning it specifies exactly which type we can use we can't use anything but that type so that's kind of the the foundation of generics and using generics and you've probably use these a lot so I'm assuming that you at least have done this before and it goes yeah I got that okay but now you can't understand why they're so important and why they're pretty powerful the other thing is if we wanted to have create a list where there was a list of int and a list of string and we didn't have generics we have to create multiple overloads for the list in fact if you look at the console.writeline you'll see you know 19 or 18 different overloads so if you look at the list there's 19 total different types because it takes in a boolean a char a char array decimal double they try to account for all these different types well with generics we don't have to do that we can say whatever you pass in that's cool we'll take it so that's some really powerful stuff and so let's get into how we can take advantage o beyond just using what Microsoft has given us and I want to preface this by saying probably you won't have to do this very often typically you just use what Microsoft has given you but now you know a little bit better about why you should use it and why it's important but there are times when you have code that would benefit from generics and so I'm going to demonstrate one of those cases so let's let's just comment this out so you can have it we don't need to actually do anything more with that but what I've got here is this demonstrate text file storage I call this here and then we'll look at what it does so demonstrate text file storage creates two lists okay so we have a list of person and a list of log entry notice these are generics so this list a person is different than this list of log entry okay each one holds one specific class type so you have people and logs they also have two file paths and we're going to actually gonna save things to CSV files and actually load them as well so the people file will be people dot CSV and that's in a C temp folder now if you're running as a home the the code will be on my blog so you can go ahead and there's a link in the description below but you I blog and download the source code visit the start and the finish if you are going to run this just make sure that you have the C colon slash temp path it will not create it for you I thought I putting that code in here to create if it doesn't exist but I figured that it would add the code and not really give us anything of value so just make sure that the temp folder exists in the C Drive and that a normal user has access to write to it which they usually do so just make sure those like though that file exists or folder exists so we're our write the people dot CSV file and also the logs dot CSV file and I did CSV instead of text because that way you can open up an Excel or a notepad so either one now first of all this populate lists is just a quick method that adds three people and three log entries that's just kind of a here let's put some stuff in there so we can save something to the text file and then I haven't done both the I've only implemented the people and throughout we're gonna look at what this does and they're going to implement the log file as well so I've done I kind of saved us some time but already starting this process so I have this original text file processor class and I call it this because we're gonna have a new way of doing things and I didn't want to kind of name the same thing so we have is without generics folder and in here is the original text file processor we'll look at that in just a minute but we have save people okay so the save people takes in a list of person and it takes them the file name of what we're going to save it and so it's going to happen as you can create a CSV file called people dot CSV and put the data from the people list the list of person I'm gonna put that data into the text file so let's look at that particular method so if we go to here and look at the save people we create a list of string that's all of our lines so the way we do this is let's skip to the end real quick just read outlines so system diode filed outright outlines you pass in we're gonna array them to and then a list and so that's what create here is our list and then we'll add a line for each person in the people list so the first thing I do is actually create a header so I say lines dot add first name is alive and last name now that's a kind of a weird order and the reason I did in that order is because later with generics it's going to create the the header for us and it'll store it in this same order so I didn't wanna step on toes of the generics so I did it this way you wouldn't have to do this normally if you were doing it manually you could do it in any order you want but since I load it up with generics I'm going to create my own order so or mirrored that order alright so first name comma is a live comma last name and these by the way these the list of per person and also the log or in the models folder so as the person which is just first name last name is a life these are like the bully and just to show you something different and then the log entry is an error code which is an int message which is a string and a timely event which is a date/time also note that it defaults to date/time QTC now that way we don't have to put a date/time in it just creates it for us so that's that's those two anywho back to this so we create the header row first and then we loop through each person in the people list I call it P it's a short weave in four H's it's usually helpful to clear a very short variable name that way it doesn't make your lines ridiculously long when you're trying to type them out so I'm gonna add and create this so that's this is the lines I'm gonna add this line right here which uses the the dollar sign which allows us to insert these clear braces and put values in their place so first name comma is a live comma last name so just like this pattern here only these are the values not the header and then it writes that to the place we we tell it that's it so that's the original text file processor saved people now you may have already noticed a problem but we're gonna go on and I'll show you the problem in just a minute it will run but there's another problem here so what I do next is I said okay now that you've saved that let's load it so load people pass in the file name and put it into a new variable so this is a blank variable not has not been created yet notice i use the VAR keyword here typically i don't but in this case one of the things that does is it shortens the declaration so instead of saying list of person new people which would make this line a little longer I say go to the bar so it's one of those things that it's up to you typically I don't use var that's one of those cases where it just shortens things up especially these really long names and if you mouse over it it still tells you the actual type because c-sharp is strongly typed and so we we can tell exactly what type it is all right so once I load those people will look at that in a minute then I just loop through each person in the new people list and write out their first and last name and their status of is alive or not okay that's it so let's look at the load people load people it does it everything reverse basically so it creates the output which is a list of person it creates a new person which we'll use in this for each in just a minute and that reads in all the lines from the file path so we pass in that that person's CSV file and say read all lines make a list out of it and put it in the lines variable let me remove the header row so it's kind of important because we have that header row get rid of it we just not actually data once you remove it and that's why I did the two lists I didn't have to do a two list except the fact that I wanted to have this remove at and that's really helpful so now in this for each we loop through every line of actual data we split it apart by the comma and then they create the new instance of the person and say okay first name is vowels zero last name is vowels two and in essence basically the split so it splits it into a string array and this middle one is kind of interesting because he is alive is actually a boolean not a string we're passing in stuff from a text file which is strings so we have to do the bool dot horse and pass in valves one now if we put erroneous information a text file that's gonna blow up just just know that but since we're creating text file and we're loading a text file there shouldn't be any outside interference or at least that's the assumption now if this is an actual production application I would still put some serious error checking on this so that loops through every item and it adds the resulting person to the output list and then finally returns output so let's just see us in action we're going to save the people to a file and then we're going to load them up into a new list and then loop through that list and print out the information so before I do I'm actually gonna add a console.readline it's the beginning of my application and the reason why is because i wad the console comes up in a different window and i want to drag it and my file explorer under the same window so you can see the file get create so let's do that you gotta run it I will drag down my file explorer so this is the the C temp folder let's move my console over let's make it a little smaller you can kind of see both on the screen at the same time okay so if you keep an eye over here I'm gonna hit enter up here so I hit enter there we go and now it creates the people dot CSV file and we have three entries so this is what had been read back to us and put in the new list Tim quarry is alive is true Sue Storm is alive as false and Greg Olson is lives true right so those are the three entries and they were strongly typed they're actually in a list of type person so we're good to go if we open up this CSV file let's open an excel this is what we get in fact if you wanted to we can say marry false Smith so that's first name is alive and last name marry faults Smith we save this and close it instead of doing our save here we won't save our list we'll just load our list okay so we just load our list notice that now Mary Smith is a lot as false that information comes to as well so we've proven that we can save to a file and load from the file so that's great but hopefully you've noticed the problem and that problem is what do we do when we come back through with the the log entry well we go over to the original text file and honestly what I'd do is I'd probably do this copy-paste and say list of log entry load logs notice how it changes I'm making all right and it's not let's just change let's change the name P to L honestly log and it's not first name is alive and last name it's going to be error code message and time of event all right it's go yell all three of us are all three lines the message is a simplest because that's the one it's a string the error code is an int so we have to say int dot parse Val's or vowel zero and then this is a date/time so date/time dot parse pass in a string and there we go so now I've converted the load logs now let's go to these instead of say people I'll copy this paste it and I'll say save logs it's a list of log entry I'll call this logs oops we rename that from people of logs and then we'll make this a I think that's all we need to do right there okay now we need to instead of first name last name and our first name is alive and last name we have error code message and time off event so error code if the type is exactly right message time of event and get the right order and then we say error code message and time of event let's also change that to be logged instead of P it wasn't people it wasn't a person it's a log all right so we already had a model so copying pasting kind of help us here but that's a lot of work all right let's go back and see if it actually works so I'm actually going to comment this out I don't need to prove that the text file saving for people and loading works but let's do it again and say original text pallet processor dot save logs logs and the log files the path and now var new logs equals original text pallet processor dot load logs and it's the log file for each and we'll say for each log in new logs will do a console.writeline our dollar sign open and close double quotes and we'll say let's start with the log dot error code and then I colon and then space and then the message at log dot time event dot let's do the two let's do two short time time string and now let's give us a tie up day so that should be it we've now done the save no logs let's bring us back over and we'll pull back up our our file here notice that you have just people but if I hit enter we gonna have logs dot CSV and we have is messages the 999 I blew up at the one 337 I'm too awesome adds and a 2 2 2 2 I was tired at ok so those are the 3 and by the way that's not actually my time that's UTC time so anywho so that worked but notice all the work we had to do and if you've watched a solid series you also know that I've violated quite a few of the solid principles because I had to go back through and modify a class and make some changes and the class does multiple things it's just kind of a mess so let's see how we can do this easier so I shouldn't comment out all the saving and loading the log files and that's actually puts a a marker here just you can see it I'm going to put some you know some let's just do this make it a little easier to see alright let's say this is the old way of doing things non generics ok so if you're looking the source code in the completed source code you'll see this section right here if you want to see how it works you can uncomment it and and see in action ok and now we'll have up here the the new way of doing things with generics okay and have a code in here all right so I actually has folder called with generics which we haven't looked at yet so let's open this up and see what I've created now I could have typed this for you but this is a little more complicated and I didn't want to type the whole thing out and I didn't want to stray off into explaining why I was doing certain things the way I was doing things for things didn't really matter okay so this this code just to kind of prep it a little bit don't freak out when you see it it's a little more complicated get some you know nested loops and stuff like that but that's not what we're concentrating on we're concentrating on how generics can keep us dry that dry ISM don't repeat yourself help us with applying solid although solid principles and also how to make our lives easier so let's look at this code where I have two methods load from text file and then I have saved a text file that add quite a few comments in here so that as you're reading through it later you can see what I did in order to kind of help you maybe some things that were out to the scope of this video but you might be able to use somewhere else so I use reflection in here I use some other things to help us out okay so I've created and let's look at the load from text file first this load from text file says the return type is a list of T as an actual T it's not a type now list of int now as a string it's type T and I made that up okay you don't have to call it T that's the default common syntax is T I could have called it a I could have called it be alright so I said it returns a list of T and that's there's a loads from a text file and then it has the the again angle brackets and T so this is a generic method so that when we create it we can in any actual type in inside these angle brackets and we can use it alright so he load from a text file and we're passing the file past it looks a lot similar to what we did in the without generics but we're not specifying the log entry class with a person class we're specifying T instead now there is this limiter here and this is kind of important it's one of those things where again it's a more advanced example but I want to show you a more exam a more advanced example because I wanted you to see all the things you could do or might need to use when using generics so T could be anything it can be an object it could be an int you get a string but that's a problem for us because we're gonna loop through and look at all the different properties and save and then load them well a string isn't half properties not the same way and so I'm in teether and study a problem and so what I want to do was say yes this is generics but you can't just throw any type you want in here I'm gonna limit you so right after the close paren before the opening curly brace of this method I can say where t and that's the type so if I call the a a B where a but where t and then I colon and then here's the limitations so the first limitation is it has to be a class meaning it can't be an int it's gotta be a class so that's the first one and then comma because there's a second limitation the second limitation is you have to have the ability to have an empty constructor they may think well every class has an empty constructor and that's not true because if you create a class and say the only constructor on it takes in two parameters that does not have an empty constructor then this one is important I'll show you why some times okay not always in fact it wouldn't have them important in this class and if you go down here to this one nope I actually put on both okay sorry it wouldn't have been important except for one line in this in this file so you don't have to have this but these limiters allow you to kind of dial in what type of object is putting that T now in here I'm still loading in reading old lines and putting it into a list of string and then I create my output but instead of my output being a list of person or a list of log entry I say it's a list of T I know I don't know what that type is but I know whatever that type is it will be put in this place so over the list of person this will become a list of person all right I knew up that list and here's where the the new requirement comes into play I create a new instance of type T and so I say T I give the name entry equals new T with an empty constructor see if I didn't know that this class had an empty constructor then I can't use that I can't say new it up because I'll have a compile time error let's actually show that so if I were to take off that requirement of being new that says I'm sorry you can't create an instance of the variable T because it does not have the new constraint you have to specify they could have the new with an empty constructor in order to actually new it up all right so I wouldn't have had to do this except for reflection now what reflection is and this is Kyle says Scopus video but I want at least cover a little bit reflection allows you to look at an object at runtime and get the properties all right so I'm saying this entry variable which is just an a new instance of T get the type that is so we'll figure out what the actual fit type is not just T but the actual you know is it a person is it a log entry what class is it get that type and then get all the properties for that type and put all those properties in calls so if it's a person it's gonna find these three properties and put first name last name in is alive in that VAR calls okay again you don't have to know the ins and outs of how this works I'm just trying to give you an overview in case you want to come back and use this as well reflection is a little expensive so don't just go oh cool a new thing to put a tool box and use it all the time this is a don't use it very often use it in limited circumstances and be careful about your performance now for reading and writing to a text file when you're you know saving things out of memory in out of the disk and loading things from disk in a memory that's probably a great time for reflection if you need it because you're not doing it all the time you're just doing usually it you know you're saving your application before it closes or when you load your application of a start up so just know that reflection which is what we're doing right here where the get type dot get properties that's a little expensive as far as speed and I kind of notice in this application because it's not like oh my goodness it takes five minutes but it does significantly slow down your application so just kind of know that BEC your mind don't use reflection all the time okay let's go on to what we're doing next I'm checking to make sure that the lines count is less if it's less than two remember that we have a header row so if you have less than two lines what do you have just the header or nothing at all so the lines is less than to throw a new index out of range exception the file is either empty or missing okay so you load up the file and that's the lines variable but there wasn't at least two rows the header row and at least one data row therefore you get a problem all right so that just XS out right away so now they can be safe knowing that we have at least one data row so we can look at the row at line 0 and split it because that's our headers and then I remove that Rose was just now lines just has the data same thing we did in the original text file processor then we loop through every line and say creates a new instance of T then he's split each row and then we find out find the matching column okay and this is a little trickier but what we're doing here is looping through and I could explain it here but what we're doing is looping through and finding the header that matches the columns name we find it then we can set that value so it's essentially saying okay got first name for the column called up name is first name it's gonna loop through the headers until it finds the one it says first name we match up then we're gonna set the value for the first name value we're gonna set it to passing in the value that we just found okay so it again a little complicated don't worry about if you don't get that's okay we're not going to concentrate too much on what this is doing more matter of showing off how we're using generics okay so the big thing here too is we're converting that value to whatever type that property is so the boolean we're converting a value to a boolean if it's a daytime and convert to daytime and so on all right we add that entry to the output return output save is kind of the same in Reverse and you can read through that again the the the owners here isn't on understanding how the method works as much as seeing how we're using generics okay so this is actually pretty valuable code because you can use it in a lot of places and in fact if you have purchased the the c-sharp application from start to finish course or have done on youtube for free and kind of recreate that code this is a really big upgrade to the text file data access so a text file data access follows is this original save people save logs load logs load people kind of thing where every single thing has its own method or actually to one to save and one to load you can kind of replace all that with this right here all right you can actually just drop this whole thing right in without really touching anything just copy and paste and it will work alright you'll have to put it in every one of those methods and that's kind of outside the scope of this video as well but but then you'll have to write all that code whenever you create a new class it's just again point to the same two methods so anywho the sage a text file again passes in the T it takes in a list of T and it says T has to be a class and it has to have a empty constructor now I don't know it's necessary in this one let's comment it out and see if anything yells s and it does not so therefore we don't need it on this one let's just make sure by doing a build here built it see it alright so let's take that off no need to have a limitation that is not necessary the only reason why you might say well it's kind of necessary is because you can say some of the text file but you possibly could not load back out so if you had a class that didn't have an empty constructor you could save it to a text file but you couldn't load it because it doesn't have an empty constructor so that might be a reason why it's a well I kind of want to keep that that new on there up to you all right so again this is all commented but the the key here is just seeing or passing in the T and utilizing it as T not as a specific type okay so let's run these and we'll come back and look at some of the nuances of how I'm using a different waste so let's start with the safe all right so we could say instead of original text file processor will say generic text file processor dot sage a text file we need to give it a type and so we'll say this is the person type they pass in a list of person so a be our people list and our file path people file and we're done okay let's do again for the locks save the text file same method this time the log entry instead logs log file and we're done now let's read them back out I'm actually going to copy this code right here because the the for each looping through that's kind of same stuff all right so there's a logs and we'll grab the the people as well so let's uncomment these lines okay so but they're going to change though is instead of doing the original text file processor will do generic file processor dot load from text file we're gonna load a person and we'll say people file and down here instead of original text file processor will say generic text file processor dot load from text file log entry log file that's it notice I didn't have to change anything that class even though I passed in different types it's not a problem okay so let's run this just to make sure it works so again we still have that that pause in it first I'm only using the new generic text file so there's we're saving it to two texts files and then we're also loading it from text files so let's run this I'll pull up my my window here and let's go ahead and delete these and let's run this and watch the files pop up there's those files and there we go so it still works it works the exact same way as far as the visible output so the visible outputs the same the files are the same if we open these up we can either open them up in Excel or in so those are our time our message and our error code works the same way but this time it's super simple if we want to make a modification so say we add a third model not a problem we could just add a new model creer list pass it in as that type and a font the filename for that new model load it back out and it all still work so really simple stuff and as far as using it goes yes it gets a little more complicated too make one but the payoff of actually having this is worth it so if there's a case where you have you know something like this one of the cases with the I use it for is I used a per and so if you see my video C sharp connecting C sharp to sequel the easy way I demonstrate how used a per I also use it in my c-sharp applications course but with dapper it uses generics and one of the things you can do is say this is the type that's going to return so I do a query I can say query of type person and it brings back the data it will create a list of person put the values in and it's doing something very similar to what I am doing in in here so so it's doing very very similar so but the problem was I was creating this you know open the connection you usually use this event and get the connection string and load up all different parameters and creates the the code for actually calling all the rest it's about 10 line of code if you do everything and I was writing the same ten lines of code over and over and over again and I thought why am I doing this and so I actually created a generic class or generic method that's I just say okay here is the stored procedure here is the the type so say person or log entry whatever I passed those two things in along with the parameters and it it connects to the database connects to the right database and does all the work for me and so that's a really easy way where I don't then have to kind of write the same code over and over and over again I don't have to say man there's fili a dry violation or I'm repeating myself a little bit but it's all a little bit differently different not a problem now I have one one method and that's it so there's definitely places to use this and it is a little more advanced sometimes okay now this one is a much more advanced case just to kind of show off the different parts of it okay so I have the where where I'm limiting what T is and returning the type and also passing in and I'm creating a new instance of that type there's more advanced cases okay a lot of times you'll just pass in the type and then maybe have it as one of the parameters or the output okay so that's that's a more common usage this is a little more advanced all right but this kind demonstrates all the different things you could do in one method or two methods so I did want to show it off now the one thing I don't show off that you could do is you can pass in more than one type that's generic so I could say t comma I was passing you alright and that would work I could pass in a you type and it could be any type you know it could be an int it could be a bool or if I said no you know what that you needs to be something locked down then what I do and that's high definite I could say after I've specified what T is I could say space where u is I'm gonna say a class and that's it okay and there's other things you can do besides class and new but but those are the kind of common ones I've used so you can actually even limit this even further let's just say where it's a person okay now that has to be the person class is that a wise idea probably not but if you had an interface maybe it's an interface so it's a I person and they could pass in any type any of those interface possibilities so there's a lot of things you can do here and you can limit both of them you could pass in to get passing three can pass in for so again lots of options with these generics but the one thing I would point out is you're not gonna litter your code oops I yeah there you go you're not gonna litter your code with your own creation of generics there's probably a MB you know fifty a hundred different places to take advantage of generics unless you're in a pre advanced application most likely for small to even somewhat moderate applications there's not gonna be more than one or two places where generics might improve your application but when you get into a larger environment or when you're creating some some coasts really trying hard to apply two solid principles and you're really trying hard to make it flexible and yet conform to open close this is a great thing to think about is do I have code that's kind of repeating but with different types and if so can I use generics instead so that's a an overview of from kind of beginning to advanced generics from just using lists of T and understanding why it's it's type safe and why it's better than a list of object which is kind of a the beginning to moderate use all the way up through creating your own generics this video can't cover it all but there's more to be learned about generics so I didn't cover creating your own list of T so you create your own you wouldn't call Lissa t call it something else but basically we have the enumerator and all the rest this way of doing that and so you could get into that if you want to the the use case for that is even smaller than what I've demonstrated here and so I didn't really think that we needed to cover it video there are some good resources on how to do that but the basics are you can pass instead of passing at the at the the method level you can actually pass in 80 at the class level so let's just call this s will you pass in at the class level a type and then carry that all the way down through all different methods so and again you can still limit like you do in the methods with the class level so as somebody that you can do but again that's even smaller window of people that might use it so I hope you found this useful again from begin to have almost the end of what generics can do I'd love to hear your thoughts on this let me know down the comments below what you found confusing what you found helpful and how you can actually improve your code going forward also don't forget if you liked the video try give me a thumbs up that'd be great it does help boost this videos performance on YouTube and and allows more people to see it alright thank you very much for watching and as always I am Tim quarry
Info
Channel: IAmTimCorey
Views: 249,179
Rating: undefined out of 5
Keywords: .net, C#, Visual Studio, code, programming, tutorial, training, how to, tim corey, C# training, C# tutorial, c# generics, c# generics advanced, c# generic list, c# generic methods, c# generic method example, c# generic types, c# generic classes, c# generic constraints, c# generics tutorial for beginners, generics in c#, c# collections, collections in c#, c#.net tutorial
Id: l6s7AvZx5j8
Channel Id: undefined
Length: 55min 32sec (3332 seconds)
Published: Mon May 07 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.