How C# 12 Changes Your .NET Classes Forever

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody I'm Nick and in this video we want to talk about how one of the seemingly least important C 12 features has the potential to completely change how we write idiomatic cop in the same way that file scope name spaces and top level statements completely change how we write our code now I want to talk about it in this video because I don't know if I like it and in this video we'll discuss that and explain why I think it's a bit of a weird thing to have in the language especially based on how we write code now days if you like content and you want to see more make sure you subscribe for more training check out my courses on do train.com now quick announcement before I move on we just launched a brand new course on Dom train called getting started with clean architectur in.net and that course is authored by the excellent educator Amai mband you probably know him from YouTube link in the description if you don't already now Amai works for Microsoft and he works on technology that powers tools like PowerPoint and office in general as well as teams so you know his code is used by millions or hundred of millions of people every year and after all the research he did when he made this course he said and to quote him this is the best clean architecture course you're going to find out there for net Now to celebrate the launch I'd like to offer the first 500 of you a 20% discount code so check the link in the description and use the code you see on your screen right now or check out to claim the 20% discount and two more things there will be a deep dive course coming on clean architecture so if you buy this course you're going to get a special discount code for the Deep dive as well as long as you subscribe to our mail list so go on D train scroll all all the way to the bottom and put your email down we're going to cross check everything and we're going to send you a code and if you also want to get started with DDD which can be p with architecture during checkout you can add that cost as well for discounted price too all right enough with that now back to the video all right let me show you what I have here I have this simple console application and ultimately the feature I want to talk about is called primary Constructors now you might have seen primary Constructors and we kind of had a flavor of this in records before cop 12 but now they're coming for traditional classes instructs and they look like this we can have a public class over here let's say user traditionally you would have a Constructor over here which could have some parameters for example string name and also date only date of birth now these would be parameters you could only use within the Constructor and they would not bleed outside of the Constructor in any way if you want to use them outside you would have to have either a field and assign it to that field or you would have to have a property and assign it to that property it makes sense now with C 12 what you can do is you can actually take this and put them on the Constructor level on the class declaration that way you can go ahead and start creating users like this you can say new user Nick jopss and then you can have a date of birth and that is it now at this level they are still Constructor parameters however you can actually use them in many ways for example you can create a property over here for example name have it be a getter only and then assign that parameter from The Constructor to the value of the property and if you know how properties work behind the scenes they actually have a backin field so that is assigned in the Constructor on the backing field in fact if I compile my code over here and I go to the I viewer you should be able to see it over here in the lower C shop so the name parameter the name property has the backing field and that field is set to that value on the Constructor so that's how our code would actually go from high level cop to lowle cop now interestingly enough you can have other things over here for example you can compute Fields if you want to if I want to have for example an age field and this is totally not how you should do age Fields but what you can do in here is you can say date time do today do year and you can say date of birth do year um sorry not equals but minus to get the age this is not how you get the age this will give you actually a bad value and you have to account for things like leap years but for the sake of this video let's say you can do something like this and if you want to be computed on every time you actually call age you would have a computed property like this also at this stage we still working with backing Fields but if you wanted to use let's say the date of birth um in a method you can say public void test or example here and the moment you say date of birth here and I can actually do something like this then if I compile code and I go to the lower cop code again then this time you're going to have a field generated for that parameter to store it not in the same way as the backing field for the property but dedicated field for this Constructor parameter itself so ultimately what I'm saying is the compiler will do a lot of magic behind the scenes to account for anything you might want to do with this primary Constructor now before I explain how this will fundamentally change how we write cop I want to point out that this has nothing to do or behind the since has nothing to do with how records work so if I have a user record for example that has the same signature well this is really not the same and the reason why it's not the same is because these will be behind the scenes properties true properties not just potential fields or Constructor parameters but as you're going to see over here they're going to be properties let's just scroll all the way up the way these things are lowered is a lot but you can see them over here we have a name parameter with a getter and an init only Setter and then the same thing with a date of birth so do not confuse primary Constructors in classes and structs with record class Constructors and Constructors of record structs it is completely different I know it's confusing what do you want me to do now why do I say that this will completely change how red SE shop well let me go to an existing project I have over here which is a weather API from a previous video and if I just go to program.cs nothing really changes but if I go to any of the other classes for example if I go to this open weather map service well everything looks normal on the surface but if you actually can potentially see it I have a gray color over here in my Constructor now in my ID and I have a note saying convert into primary Constructor so I have a new refactoring I can use to convert this to a primary Constructor and now my Constructor is removed my injectable Constructor is removed and is being moved all the way up to the weather map service so now it is here any of my classes that has any service like this injected and no particular logic in the Constructor will have this option now if I go here to my cast weather Handler I can do the same thing and both of those things now go here now this does remove that Constructor that code that you have there every single time however to me it does feel a bit weird because now I don't know how to structure my code previously I could very easily see that this is a class implementing or extending another class delegated Handler and I could see my Constructor clearly and when the Constructor gets too big I might actually newline it um in this fashion over here Some people prefer this Some people prefer this pick your poison I don't care but now if I do this then all that moves to the Constructor up there I lose half of the parameters and this only has two I've seen Constructors with five injected things like you wouldn't even know what you're injecting if you were using this view plus I don't know what interfaces I'm implementing or classes I'm extending for some reason my eyesight starts here to see what happens to the class and all I'm expecting here is interface or class is being implemented or extended so I guess that might be me having to retrain my brain but I can totally expect this being the know from now on so what are we doing well you can say do something like this and I'm like okay like but is it this or do you keep the colon here and then you do something like this but then do you know that this is not an injected thing and it's an extension thing and should it be like this or like should it be more like the like I don't know how this will go and that's where you come in as well in the comments please let me know what option you would prefer I'm going to put a bunch of verion now on the screen and all of them have a b c d or whatever choose the one you prefer and put it in the comments down below to see which one is the most popular one and if you don't mind explaining why you prefer this version please write that down as well now I do think that this is a good change overall and having the option of a primary Constructor is a good feature but I don't quite know how I feel about this given I have this platform and this community I'd really like to know your thoughts so we can potentially come at a conclusion that we all agree with well that's all I had for you for this video thank you very much for watching and as always keep coding
Info
Channel: Nick Chapsas
Views: 78,691
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 8, C# 12, c# 12 features, primary constructors, primary constructor, primary constructor c#, primary constructor c# 12, How C# 12 Changes Your .NET Classes Forever
Id: 93PHtu0B47s
Channel Id: undefined
Length: 9min 4sec (544 seconds)
Published: Fri Sep 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.