Using & Creating C# Extension Methods

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
with c-sharp extension methods you can add functionality to objects that you don't even have the source code for how that works let's go check it out in this video so here we are in visual studio 2022 i've just created a file new console application and i've added two classes so i've added a product with a simple name and a price and a shopping cart which holds a list of those products now using these extension methods can be particularly useful whenever you cannot change the actual classes here so whenever you maybe downloaded a third-party library and you're using objects there but the author of that library didn't really implement the api that you were looking for so whenever you don't really have access to the code and you can change it because what i'm going to show you now can just as easily be part of the shopping cart of course because you have total control of all the code here but imagine that you know you're installing some kind of other nuget or getting code from a third party that you cannot influence you can add extension methods to those as well and that is what's really cool of course you know if you just like the concept of extension methods um then you can definitely use this for some scenarios as well um from the top of my head i don't really know anything um i can't really come up with with one but you know sometimes you just have that feeling like hey the extension method might be nice here just add it i mean there's no law for it just do it whenever you feel like it okay so for the shopping cart now let's just create here in the main so this is just the console application because it's not about the output it's about the actual code here so let's create a shopping cart right here shopping cart is new shopping cart and whenever we have that we can now add products to it so let's do shopping cart dot products dot add new product and i don't need to actually initialize it and add the constructor to it so let's just do this and we can initialize it with the properties directly let's give it a name is i don't know what's this product name maybe a youtube subscription to a channel that you like so youtube subscription it should be um maybe let's add the comma here and the price would be i don't know 13.37 it's pretty elite a youtube subscription let's add another one let me just copy this one and let's add another product here so maybe you want to add a product being nice and being nice costs nothing so that's zero it's for free and maybe we want to have another one which is going to be i don't know something that you would want maybe an xbox which is 42 just because 42 is the answer to everything now we have three products in the shopper card but maybe you want to know the total price so now you can loop through all these products in the shopping cart and you will know but you know if you want to know it in a different place in your code then you have to loop through it again and then you have duplicate code which is not something we want so let's see how we can fix this let's create a new class so mind you this is all in the same file but the line that i put it here in the namespace so this is just a separate class it should be in its different different file well you know also that's a matter of taste basically but that's typically how it's done just for this reference of this video i'm just putting it all in one file here so also this one goes into the namespace and what you want to do is create a public static class shopping i mean you can decide on the naming shopping cart extensions that kind of makes sense and then you also want to create a public static method and it's going to return a double in our case get total and here comes kind of the the the magic trick that makes this all work so as a first parameter you have to specify this and this is referring to the object that you're going to extend and then you're going to follow that by the object that you're actually extending so in our case the shopping cart so this shopping cart tells us that this is a extension method for the shopping cart object and now you want to add a little name so we can actually reference this variable inside of our method so let's make that shopping cart as well and now we can just reference this shopping cart from this this little name right here and we can say for each of our product product in shopping cart dot products now you might want to add some error handling here maybe null checking maybe see if there's actually products in here but this is just a regular method you can do whatever you want in here and what we also want to have of course is a total price so let's make double total is zero and then we're going to say total plus is product dot price see i already get some helping methods here so this is going to add up all the totals here and what we then want to do is say return total and we get the total of our shopping cart so again this is just a regular method but make sure that it's in a static class it's a static method um and you have to have this special this shopping cart thing now the really cool thing is you can also add more parameters to this so if you have bool um in euros i don't know something or other then you can add that here as well so you can just add multiple parameters in this extension method if that's what you want so let's go back up here where we are actually filling the shopping cart and let's use this thing so what we can now say is shoppingcart dot get total here you can see we have the get total and it says extension method and it has this little other icon here so that you know that it's an extension method now another important thing to note here is especially if you're using this with like that third-party library that i mentioned it might not be in the same name space so it is a little bit hard to discover those extension methods so if it's there i think the intellisense will help you maybe uh if it if it can find it there but you have to really know at that using at the top um with the right namespace to make those extension methods really show up in your intellisense here in this case it's all you know in the same file in the same name space so it's no problem but that's something that you want to be aware of now the interesting thing here is if you look at this little method signature here is that it only shows us this parameter bool in euros and the other one is because we're calling it upon this shopping cart here and it will automatically provide this shopping cart instance as this parameter because we put the little this keyword in here now another really cool thing if for some reason you want to call this extension method not on the shopping cart i've ran into this scenario with xamarin forms where i still wanted to go through the extension method but i couldn't call it on the actual object i didn't don't recall what the actual scenario was but you can still call this method um with like you know shopping cart extensions and then say get total and then you can supply the shopping cart itself so you can still call it like this as well with um true and then it will still kind of work now i chose quickly is because i need to then comment out this this shopping cart here but whatever you do you can see that it works like this as well so you can also still use it as a regular method if that's uh if you have a scenario for that but just just a little pro tip right here and the really cool thing that we want to do of course is just do the get total here and um then well the in euros doesn't do anything but let's supply it with a little value there and this is then how you would call the extension method if i lose the extra parameter here then we just can do it like this and we get the total just by calling get total so let's get this in a console.writeline and let's make this total in cart and we have to do it like this oops missing a little bracket here and a quote and then we have everything in line let's add another console.readline here just to make sure that our window stays open read line and we can see the result and now whenever i run this a console application should come up go over through all the products here you go and we can see that this is a 55 37 so it just adds up the ding nicely with our extension method and that is how you can extend methods that you don't even have control over in c sharp now we've learned what extension methods are in c sharp and of course all the code was you know written by us in this case but it also works for classes that come in from new get packages that you might bring in or maybe you have some project reference or dll from some other project that you put in there these extension methods will work on those as well which is pretty amazing if you think about it that you can just extend the apis without touching the original code because for some reason maybe you can't please let me know if you want to know more about these basic c sharp concepts or net concepts let me know down in the comments other than that thank you for watching another one of my videos please click that like button check if the subscribe button is lit up as well so you're subscribed to my channel and you will automatically be notified of new content other than that i'll be seeing you for my next video keep coding [Music]
Info
Channel: Gerald Versluis
Views: 1,347
Rating: undefined out of 5
Keywords: extension methods, extension methods in c#, c# tutorial, extension method, learn c#, c# tutorial for beginners 2021, c# tutorial 2021, c# tutorial for beginners, learn c# for beginners, learn c# programming for beginners, .NET, c# language tutorial for beginners, c# language fundamentals, c# language tutorial, c# language basics, c# language learning
Id: D_FiBoJNhTE
Channel Id: undefined
Length: 10min 12sec (612 seconds)
Published: Thu Sep 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.