What are Generics? (C# Basics)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to learn about generics in c-sharp this is an extremely powerful feature so you can build a specific class and make it work with any time for example I use generics extensively when making the grid system so that each grid position can hold whatever custom data you want let's begin [Music] hello and welcome I'm your code monkey and this channel is all about helping you learn how to make your own games with NF tutorials made by a professional indie game developer so if you find the video helpful consider subscribing so generics are a great feature of c-sharp in order to help you write more generic code that can be used in many scenarios what they allow you to do is write your class or functions in a way that you don't specifically define any particular type so then when you use that class or function you can pass in what type you want it to work with you can define an entire class or interface as a generic or just a single method or delegate now you might have never heard the word generics but if you've written some code then chances are you've used them if you've ever used a list then you've probably used a generic we need the finalists you also defined it to the type of elements that will go inside of that list that's the generic parameter you can make a list of any type you want another example is when I made the grid system in a previous video I also made it work with generics meaning that that system can hold whatever type you want in each word position yet another example is the video where I covered handling events in unity dots in the end I showed a nice class I built which uses generics in order to be usable in any scenario so first you define the event component with on with the data that you need and you pass that in as the generic type by doing so you can use the same generic class while having it be customized to the particular event you're trying to capture this video is made possible thanks to these awesome supporters go to patreon.com/scishow to code monkey to get some perks and help keep the videos free for everyone all right so let's look at how generics work okay here we are in an empty script let's start off simple and just make a function so we're going to make a function that takes two elements and simply returns an array containing those elements so when it's right now so we make it private and now for the return type let's make it return an int array and we're going to receive an int for the first element and also an end for the second element and inside all we're going to do is return a new int array holding both of our elements here it is very basic and now up here we can call our create the write function and we pass in our two nice ends all right so here des is very basic let's test any of there we have our log so we have two almonds first five and then the six okay so here we wrote this function in order to solve a specific problem and it works great however now let's say that we need to solve the exact same problem but this time we wanted to use strengths so naturally we cannot do create array and then pass in a string and then another string here obviously we have errors so what we can do is duplicate our code and make another function but this one that works with strings so just like that now this would work but you can already see the issue if we keep doing this then eventually we're going to need to make a new function every time we want to use it on a different time so this is where generics come in with generics we can write the function just once and make it work on any type so let's remove this one and now on this create array function instead of hard-coding the int type what we're going to do is after the function name we open and close using the greater and less than symbols and now inside we write the name of our generic type usually the standard is just right T what we're saying is that when we call this function we're going to pass in the type and that type won't be used wherever T is used so in our return type instead of returning an array of n so we're going to return an array of T and the first element will be F type T and the second element also of type T so instead of working with n so now we're working with this generic type and now up here on our function we're going to call and we're going to pass in the type in this case it's an int and over here we passing it with a string and just like that now this is working exactly the same as previously so we can add some logs just to verify any appeared is the first rays are working correctly and you can see that it's of type in 32 array and the second one using the same function is a type string array so now we have just one function declaration and we're making it work with two different types and also here the compiler is smart enough to know to infer the type so for example here we can actually omit the type definition and there you he's smart enough in order to understand that this is working with intz and over here we remove this and we can see that indeed it's smart enough to see that it's working with strings so now here if i say that i want to create an array and use the generic of type int then i pass in a 5 for the first parameter and then a string on the second parameter over here yep you can see that we have a nice error so it's telling us that the type does not match we cannot implicitly convert string onto an INT since this function is now expecting an int for the second element so by using generics instead of hard-coding our type into our function we made it work with any type now instead of having just one version to work when ends then one with floats on its strings and so on all we need is just this one and it works in all scenarios now one note here the standard is to use the name t for your generic type but this can really be any name you want so you could for example say my custom name and just like this here we have valid code that does exactly the same thing but normally you should really stick with the standard and if you want a more custom name than just T you can at least make it start with T so for example T my custom type now what you can do is also define multiple generic types so you do t1 then a comma then t2 and as many as you want so here we're defining two different types then one film receiving of time t1 and one field are receiving of time t2 so we can test that up here test multi generics let's say that the first one is an INT and the second one is a string so yep just like that any of everything works the first element is of type in 32 and the second one is a string now one use case of generics that you've seen me use before and that I covered in the delegates video is the action and func delegates so they are inside using system and you have a action and you have a pump now we can inspect the definition so you can see that the normal action is just a basic time we get with no parameters however over here you can see that you have a whole bunch of versions that use a lot of different generics so here is one example of an action that takes two different types of two different parameters and you can see that you have versions with tons of parameters and again you can see that they match the standard naming convention of t1 t2 t3 and so on now the other one is over here the func and you can see that it takes at least one time for the parameter which is the result so if we inspect we can see that it's a delegate which returns T result which is the type that we define it in here then again we have tons more versions that define the number of parameters and the type of our result so you see that you can also use your nards when working with delegates which is what these two are doing so you can make your own delegates and make it work with 81 and 82 just like that so over here we have the fine exactly the same thing that we have in our action and then the func over here is pretty much the same as this just like this so we define a type of tea result that's what we return and then a t14 our single parameter if you want to learn more about delegates check the video and linked in the description so over here we already covered using generics with functions and with delegates now we can also use them when writing an entire class so in here we can make my class and again we define a generic so we're defining the class I make me work with a generic type and now here we can use that type anywhere in our toys for example of type T and let's call it Val so now up here we can create an instance of myclass let's say we want to home intz we're making you my class and then if we access my class and try to see our family field and yep you can see that it doesn't need have a field in this case of type int and now within our class we can use the type T on any functions or fields or anything we want so for example we can use this exact same function that we used up here and now on this function we do not need to define the generic by default it already knows that the T is going to be the T that is defined when we instantiate the class now another thing we can do is also add constraints on to our generics for example let's say we have an interface so here we have a simple I enemy interface and just has a damage method and now here in our class we can receive a generic of type T and then we can add a constraint in order to ensure that whatever type we use for T it must implement IME so we do my class where T implements I enemy and now since T won't have to implement a enemy that means we can use the functions defined in the interface by directly accessing our team for example here we can make a my class constructor going to receive a T for our value and then we can go into the valley since it's of type T which implements a enemy and we can call damage so up here we have some valid code and now up here if we're trying to create an instance of our class using an int you can see that we have an error since our in parameter does not implement the I enemy interface but now we can make a class that does implement that so now here we have two separate classes that both of them implement I enemy and now we can use any of these on the way up here on our my class as our generic type just like this so I can make in my class of type enemy minion then I can also make one of type enemy Archer since both of the types actually implement the eye enemy interface so just like that and if we run this we can see that it will indeed call the damage function on the interface which won't call this one and then this one and if there it is we have enemy minion damage in and me Archer damage now there are a bunch more constraints that we can add to our generic type you can check the page in the official Docs for all of them for example you can see where T is strapped in order to ensure that team must be structs you can also do class in order to ensure that it must be a class then you have new this ensures that a team must have a parameter unless constructor then you can also combine constraints so for here what we're saying is that type T must be a class it must implement a enemy and it must have a parameter unless constructor so with constraints you can make your generics be as constrained or as free as you want now lastly you can also use generics in interfaces so down here on our eye enemy interface we can also use a generic type so let's say T and then on damage we receive a parameter of type T and then when we implement our interfaces we must also define the type so we need to correctly implement the interface down here and let's say we need to implement my enemy that receives them in and yep just like this now we don't have any errors so now this interface works the same as any other generic types so you can use it as a parameter you can use it as a return value or in this case use it as a property all right so here you'll learn now about generics in c-sharp as you can see this is an extremely powerful feature that helps you write cleaner code with fewer dependencies you can write one class that's almost one specific problem and use generics so that it's reusable in many different scenarios this video is made possible thanks to these awesome supporters go to patreon.com/scishow to code monkey to get some perks and help keep the videos free for everyone as long as you can download the project files in a Tony's from EMT code monkey comm subscribe to the channel for more unity tutorials post any question you have in the comments and I'll see you next time [Music]
Info
Channel: Code Monkey
Views: 56,855
Rating: undefined out of 5
Keywords: c# generics butotiral, c# generics, c# generic method, c# programming unity, code monkey, c# beginners tutorial, c# programming, learn c#, learn unity, brackeys, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 2d tutorial, unity 3d, unity, game design, game development, game dev, game development unity, unity 2d, unity 3d tutorial, programming, coding, c#, code, software development, learn to code, learn programming, c# basics, learn c# basics
Id: 7VlykMssZzk
Channel Id: undefined
Length: 13min 47sec (827 seconds)
Published: Sun Apr 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.