The New Extensions EVERYTHING Feature of C# 13!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody I'm Nick and in this video I'm going to introduce you to the biggest feature coming in cop 13 that will fundamentally change the way we write cop just because of how many places we can use this in and that feature is called extensions now you might have seen this as extensions and roles or roles we're going to focus on the extension aspect that was announced in build because we still don't know what eventually will make it in C shop 13 again that is a few months away but what I'm going to show you today will make it into the language and it will basically completely replace extens methods as we know them just because of how much more versatile and Powerful it is let's take a look at some code and see how the fual is going to work if you like content you want to see more make sure you subscribe for more training check out my courses on Dom train.com okay so I'm on Visual Studio code because this feature hasn't been merged in Visual Studio yet or Rider or any ID but the code I'm going to show you will be valid C 13 code now I will keep the example extremely simple just so we can understand the feature itself so let's say that we have a class called person like this one the very important thing is that we don't own this person class let's assume that this lives in some other assembly some other Library some new get package some code we don't own and by the way it could live in code we do own but in this case we're just going to pretend like we don't own this I will still keep everything in a single file just so you can have a better view of what's going on and let's say I want to say V person equals new person over here and let's say I want to get the age of this person I have the date of birth birth but I don't have the age so what if I want to say VAR age equals person dot what well you can say person. date of birth and then I can say date time do today. year minus the person. DAT of birth. year and this is not normally how you would do this you would also account for leap years it's a bit more complicated but let's just simplify this for the sake of this video so this will give us the age because we get an offset from today all the way to the date of birth makes sense but if I want to get the age in multiple locations I don't want to have to calculate it every single time like this because what if I accidentally mess up the symbol or mess up this symbol like you can accidentally introduce mistakes if you don't create a method for something like this and I could make a method within this scope only but what if I want everyone that can use the person class in this ass to also use a method to calculate the age well I can very easily do that by introducing an extension method so what you would usually have is something like this you would create in your own assembly now a public study class called person extensions and you go with a public static int get age and you'd have to create a method an extension method and you would say this person to explain that you're extending the person and this is a static method in a static class very important you're very limited in many ways and it has to be a method it can't be anything else and then you would be able to take this move it here and say return this again if you want to be very accurate you would introduce leap year accuracy in here but it doesn't really matter for this video and then I would go here and I would say person. get age and I would get the Ed from the method that's really cool and it adds a lot of flexibility in our code the same amount flexibility that introducing something like graphql into your system to replace some rest apis can introduce to your architecture and that's why we just launched a 7 a half hour getting started course with graphql in.net by Michael who is the creator of hot chocolate the library that basically everyone is using to build graphql apis in net including Microsoft by the way they have chosen to use that Library instead of making their own just because of how good it is and also Michael is part of the steering committee of graphql not just for net but for everyone around the world so not only does he make the library for net but he decides with other people as well where graphql is going so the first of you can use the link in the description or use code graphql 20 a checkout you get 20% off there is just no better learning material in net to learn how to build graphql apis but the biggest question for something like this is what happens if you want to have a property for example here I don't want to have to say get age to write this Java like code in my cop what can I do well the truth is you can't really do anything because you don't own this person class and you cannot have an extension on a property until cop 13 because everything is now changing with that extension feature let me show you how we would write this using that new feature that's coming in cop 13 so instead of having this person extension study class let's just forget about it and let's just say public implicit extension and then I'm going to say person extension for which type well I want to extend the person class which I don't own but now I have the ability to extend it and this is not static the members on this class don't have to be static and they also don't have to be methods so they can be I could just go ahead and say public int get age and have that exact experience and the way this would look is I would say take the whole thing but you can now because this is not static access this dot so you can access the instance of that person like this and you can get the age in that exact same way nothing else changes it is still an extension on that person in the form of an extension method but now this in my opinion is way better it can be organized in this implicit extension and by saying it is an implicit extension it means that it is automatically added to that type you don't have to do anything about it and the biggest benefit in my opinion is that you no longer have to have it as a method now you can just say in age you can turn this into a property over here and then I can simply say something like this so my code now is way better looks way better and I can just go ahead and say person. AG in my main code and this in cop 13 will work it is brilliant is intuitive it's so so nice and of course this could be static if you wanted to nothing prevents you from having it as a static property or a static method but they can also be instances very very nice but here's where it gets even more interesting because when you have implicit something then it's very common to have an explicit something and you actually can what if for example I want to have a representation for that person if the person is more than 18 years old which in Europe is an adult that can drink and I want to get the favorite drink of that person well I don't know if that person is necessarily an adult at this point so I can't just go ahead and say uh public string favorite drink and say h get as a method you know return oldfashioned because I don't necessarily know if that person is an adult so this can be part of every person it has to be an adult and that's where explicit comes in so I can say public explicit extension and I'm going to give it the name of the thing I want to represent so I want to say adult in this case so adult for person and this looks more like a type than an extension but now I can take that the get favor drink and I can add it here and at this point I still can't use this get Favorite Drink method here this would just not compile in cop 13 but what I can do is I can say that VAR adult equals person but I can now explicitly Define the type which is going to be adult and this will be a conversion that's going to be allowed an explicit conversion from one type to another so a conversion from a person type to an adult and now because adult has a favorite drink method that's part of this instance then I can get a favorite drink of course this could also be a property so I could again say something like this turn this into a property and I don't have to own that original class that original type anymore all I need to do is just create the extension classes I want the simplicit extension to add implicitly some extra members to that class or to that type and then some explicit ones to effectively create a subcategory or a projection of that type or it is basically an extended ition because it just adds some extra ones and I can just implicitly convert here and call that method of course this would have to be adult here but you get the point now this will be an option in cop 13 now how should you feel about this feature well it kind of feels like this feature will replace extension methods because I think people as they're extending types they will want to have the option to have instance and static members and also properties and methods in a single file and this organization works very nicely you can also have other static members here and use them in that extension type as well so I can totally see this changing how we write cop and completely replacing extension methods but I also think that the property aspect of this will give a lot of flexibility on how we've been building code and allow us to add behavior in types that we couldn't before in ways that we couldn't before it is extremely nice I really really love this feature obviously we're not getting discriminated unions anytime soon but this is a nice big feature to add a lot of flavor in how we write cop now this is just a quick introduction when the feature is eventually Visual Studio I will make a more in-depth video going through all the educ case and what you can and cannot do but I want to know your thoughts what do you think about this feature comment down below let me know are you going to use it are you excited and what's the most requested feature that you would have from Microsoft I know it's going to be discriminated unions but if there's another one or maybe from another language leave it in the comments down below well that's all I had for you for this video thank you you very much for watching and as always keep coding
Info
Channel: Nick Chapsas
Views: 73,245
Rating: undefined out of 5
Keywords: Elfocrash, elfo, coding, .netcore, dot net, core, C#, how to code, tutorial, development, software engineering, microsoft, microsoft mvp, .net core, nick chapsas, chapsas, dotnet, .net, .net 9, c# 13, c# 13 extensions, extensions and roles, c# extensions and roles, extension properties, discriminated unions c#
Id: ueO5Cb3Emcw
Channel Id: undefined
Length: 10min 32sec (632 seconds)
Published: Wed May 22 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.