Generic Programming in DotNet (C#) [2021]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys it's chris from datavids we're talking about generics and c-sharp this will be a quick video and hopefully very helpful for you if you like it please subscribe afterwards with that alarm bell below and we'll see you next time let's begin system.collections.generic has a bunch of classes that are already created and ready to use for example lists so you could do list of type whatever class that you'd like for instance string my string equals new list of type string and then you could add strings to it if you were to do the same list but choose the type integer or double it automatically works then with doubles so you could add here a double and it's not just list that falls into that category we can throw in a t of any type that system.collections includes other things that are super helpful data structures full-blown data structures like cues stacks hash tables you know sorted lists etc so take a look at that now if you wanted to see a custom generic class that's a little bit more interesting so i've pasted for you here the example that you'll find in textbooks and all over the internet which is just a basic generic class where you specify the type like so and you put a property of that same type and you specified again in the constructor then you assign the value that comes into the constructor to your property and then you can do whatever you'd like with that value and it knows what the type is and so if you wanted to execute this code it's quite simple what you'll do is you're going to go ahead and instantiate by putting the name of the class giving it a value but something new is right here you're going to specify the type so integer string etc with your basic types or you could put in a custom class name we'll do both let's start with the basic uh basics type first like integer equal to new and my ide dropped in integer for me which was nice and then let's put in our constructor value here and then if we wanted to execute the right method that we have inside of it we could take our instance right and i'll do a console.readline here so that the output will pause for you to see and as you can see it's going to it's going to come in here it's going to know that this is a an integer based on t because we define t when we ran that and typed it here and here f5 999 there you go so let's show you that custom class portion now let's create another class just above this one for this example class abc let's give it a couple of properties prop tab tab in my properties sounds good prop tab tab string my property too and now let's get an instance of that let's do abc myabc equal to new abc myabc dot say prime my property equal to x y z all right now we've got an instance with the value in it and now we could do instead of int we could do abc oops i reversed these let's put a 2 there so let's do instead of int we do abc and instead of 999 we do myabc so now you can see we're telling it that the type of the class is going to be abc and passing my abc to the constructor excuse me quite simple right now one thing that's interesting is this write method so if i was to look at this right method it's going to do console.writeline this that value well in this case value isn't going to be that interesting right let's run it anyway let's just look at it see demo.demo1.abc so let's change it up a little bit one thing we could do is we could change this from this dot value to this dot value as abc letting it know that it's an abc we'll put some brackets around that and now we can gain access to those abc properties now this is a little bit dangerous because we don't really know it's abc so we're going to change this to add a constraint to it but i'm going to run it just to show you what it looks like because this will run because in fact this is an abc i mean it is an abc because that's how we called it this time so hit f5 there you go xyz shows on the screen now if i was to say um you know if i was to call this with an ins again and i call it an int here and put five here or six and run it it gave an error because you know that wasn't really a safe way to cast it anyway so i'm gonna undo all that and put it back and we'll show you what those constraints look like okay so what you could do is where t is of type abc now it doesn't have to be abc it just means that it has to have that as the base class so now i could do i could take this out here i could put it back to this dot value and i could just do this.value.myproperty2 because it's always going to be of type abc as you can see there's no underline in red if i run it with f5 xyz is printing let me show you what i meant by what i said earlier so you could have class xyz that inherits from abc where abc is the base class uh and then you could have you know something else here uh public double zzz and you could have your getter and setter and we don't have to do anything with that and now we could make this in x y z x y z so now as you can could call it with as telling it's an xyz and even though i have abc as the instantiated value here it's an xyz but because xyz inherits from abc it's still considered an abc here so this should still work i'm going to run it with f5 and xyz still prints on the screen all right so as interesting as that was i personally don't use this that often so the next thing i'm going to show you is generic methods that don't have to belong to a generic class and we'll see what you think about that let's go with the classic microsoft example this code is from the microsoft page but i'll explain it very well to you in my work i did something similar where you didn't know the class that you're coming from but you did know what the properties were so this is something that you could do like that but basically as you can see here you've got the type specified just like you did in the class you can specify the type in the method name and then that same type is used three times here now if you wanted a different type you could do t2 like that as you can see it's underlined red because it's not found but if you did t1 comma t2 then it could be different types but we want it to be the same type so we just have t repeated and lhs and rhs if you're not familiar with it is a common programming shorthand for left hand side right hand side so this is just a swap here we're swapping and because these are both the same type we're able to do it so let's give an example here let's do string abc and we've got string def and let's do swap and we'll do of type string because they're both string i'm going to do a b c d e f and now i i'm going to put a breakpoint right here ah basic types you need to pass with ref if it was a custom class that wasn't a basic type you wouldn't need to do that we'll run it real quick and now let's look if it swapped it abc should contain def now and it did def contains abc so that worked you could do the same thing obviously based on what you've learned earlier in this video with int and int and just put some numbers here nine and four if i was to run that oh got an error ah we still have string here hint run that and if i hover over abc it has nine instead of four and def has four instead of nine so that's that's kind of a quick and dirty for generic methods i'll show you that two parameter one now so if i put t and t2 now the types can be different and it's showing me purple because it's still running i don't know that swap would still make sense let's do something more interesting with that let's just uh print them let's do uh console.writeline lhs rhs and now this could be a string and let's make it hello and now this is going to be and this is how you do this just like you have a comma here with the two types you put the two types here so you do int string and they should see them both printed and i think we need a read line so that it pauses and f5 for and hello even though they're different types and even though this method did not know what the types were it still worked let's talk about generic delegates next here i've got delegate void my delegate of type t t item and you could set it up this way where you just kind of instantiate it this way and you choose your method name you don't have to spell it out like we used to and then the only place you need to specify the type is here now this may be useful in some scenarios in my experience it's only been useful for me personally when i had a whole bunch of overloads here of different types and i wanted to write some generic code not really knowing what we're going to need next so this may or may not be useful for you and i'm sure you've got some better scenarios that this would come in handy or better ways of using this feel free to include that in the comments below since this isn't necessarily commonly used by me it might still be very commonly needed for you so that's really all i've got for generics hopefully this was helpful for you maybe for those beginners and have a great day
Info
Channel: Data Vids
Views: 1,601
Rating: undefined out of 5
Keywords: generic programming, C#, dotnet, dotnetcore, dotnet5, generic methods in c#, generic methods, generic classes, generic delegates, system.collections.generic, generic development, C# generics, generics in csharp, csharp generic
Id: FKxIlhOXHlU
Channel Id: undefined
Length: 12min 43sec (763 seconds)
Published: Wed Jan 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.