Why I won’t need constructors anymore in C# 11

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody i'm nikki in this video i'm going to show you a brand new keyword coming in c 11 called the required keyword now the required keyword is a very anticipated one for me because it effectively renders constructors as a thing of the past for a very specific use case everything added in c sharp for that specific use case has led to this moment and i want to introduce you to the keyword here if you like a lot of content and you want to see more make sure you subscribe bring this notification bell and for more training check out nickchapsters.com now before i move on i want to let you know my brand new course from zero to hero integration testing in asp.net core is now out i'm super proud of it i put a lot of effort in teaching you the right things that you would see in a big company doing things right it applies to apis mvc blazer applications it shows you how you can properly integration test your application and what does it mean to integration test an application what should be replaced what should and how you should deal with that with touching on docker on playwright with touching on so many advanced state-of-the-art things you would see so if you want to buy the course the first 300 of you can use the discount code you see on your screen right now and down below i highly recommend you check it out integration testing is such a crucial part of your testing suite and it's as important and in some cases for some people more important than unit testing some people tend to skip unit testing and do integration testing just because of the value that it brings now i think you need both but i can totally see that argument so check the link in the description use the code trust me when i say these run out quickly and i hope you have as much fun taking the course as i had making it now back to the video all right so let's see why we needed the required keyword in the first place before we see what it actually does and how it works so i'm gonna go here and create a new class and i'm gonna make a new person class over here now this could be a class like that this could also be a public record of person and you know it has a bunch of full name or whatever but i don't really care about this use case i care about the class one which is the most common one right now in c-sharp record is more of a new feature so let's say we have a couple of properties one of them is a good which can be auto-generated by default over here and i can say dot new good and then i might have a bunch of other properties i'm going to focus on one to keep the example simple so i'm going to say full name here something very expected now the problem with this approach is that the full name property now and also the id are mutable they can change now if we were to use a record this would be different but we can actually achieve any need only an immutability sort of mechanism by going ahead and changing this to init and what this means now is that if i go to the program.cs and i save our person equals new person then i can say i don't care about the id it can be left to whatever the value was by default and i can say the full name of that person is nick chapster's a very common approach to initializing an object and then because that's in it only if i go ahead and i say nick chap says he's not actually nick chapster's it's michael that's not how you spell michael michael then i cannot set that value because it is a neat only value as you can see if i did change that to set then i can so the whole limitability argument within it only but here's a problem i can if i want to even if that's in it only initialize the object and the object can have a null value even though this is not expected to be nullable this is expected to have a value when the user initializes it so how do we solve that well historically you would actually also create a constructor and you would pass down the parameters you want to be mandatory that are required effectively through the constructor and say full name and now i cannot initialize that object without going through and saying nick chops but there's a few problems with this the first one is imagine if you have 10 properties that are mandatory the constructor will be insane will be so so long it's so hard to work with i've seen people like do this kind of thing to like break properties down one after the other to just i don't know it is just too much for me so i don't like that wouldn't it be awesome if we could actually maintain the same initialization model let me just go back and have something like that but force someone to provide this value on startup but all that without having a constructor well this is exactly where the new required keyword in c-sharp comes in and let's take a look at it actually hands-on to do that we're going to use sharp laptop io which if you go to the branches right now it is not in the default branch the main one but if you go all the way down here in sharp lab you will find the required members branch and what that means is i can go ahead and choose that and now i can work my way through having that support let's see how that will look like now if i go ahead and i just copy that class exactly as i have it over here and i just paste it in sharp lab this all works you can see how the properties are being lowered to their fields their backing fields and then a getter and etc which is in it only in this case and now what i can do or what i will be able to do is go ahead and say required here now this will give me a compiler error this is because currently on this branch you cannot load the whole bcl so what i can do to get around this and actually show you the compiler problems is i can create a namespace of that name and cheat i'm gonna copy that and say public class and make a fake attribute because it looks like behind the scenes that's what they're using so i'm gonna just move it outside we don't really care about that and i'm gonna move myself over here and now i can see that the required keyword is acceptable that code does compile it will actually even run if i want to i'm going to run it in a second and now as you can see this class has a required member maybe to indicate that hey this class has a required member which is probably an indicator for the compiler to throw an error if that property is not initialized and yes it also indicates over here that the required member is this now how does that work compilation wise well let's go ahead and make a public class program which is what we're going to use to fake an execution i mean it's not fake it's still going to execute code but we need to set it up in shop lab which actually does have a run functionality over here so i'm going to say public static void main i don't need any arguments here we go and then i can create here a person equals new person and remember the required keyword is only on the full name so i'm going to create a person and i can no longer have that be initialized because as you can see over here required member full name must be set in the object initializer or attribute constructor over here so one or the other i don't exactly understand at this point what attribute constructor is however it does mean that if i want this to compile i have to go ahead and say full name equals nick chaps and now this in fact does compile i can say console.write line and say person dot full name and as you can see this code now compiles absolutely fine that is how it is lowered behind the scenes the compiler will actually still initialize your passion class without setting that value and then it's going to go ahead and set the value retroactively but it's something just the compiler can do you cannot copy that code and have a similar experience as you can see over here this is because of all the constraints we have now on the property which to me is extremely interesting because it shows that the compiler doesn't implement any complicated logic it just adds a couple of attributes just to indicate that hey i have a property in here that is a required member and this is my property so when you lower that code you can initialize that object without using the provided parameters and then go ahead and set them it's a very smart and neat way to do this and of course i can also run this in here by selecting run and as you can see nick chaps appears and you can also see the flow of the application i love shoplab.io it's such a helpful tool now the proposal for this feature also mentions other potential approaches and ways you can have a similar experience i'm going to link it below in case you want to go and see it maybe give some feedback maybe try it out yourself and see how you can help the team this is a great advantage of having c-sharp in the open you can see what's happening and you can give your feedback so what do you think about this approach is it something you would use and to be honest you would but how much would you use it how many use cases do you have for this for me it's all over the place so i'm really glad is the thing well that's all i had for you for this video thank you very much for watching special thanks to my patreons for making the videos possible if you want to support me as well you're going to find the link in the description down below leave a like if you like this video subscribe more content like sharing the bell as well and i'll see you in the next video keep coding you
Info
Channel: Nick Chapsas
Views: 135,219
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, c# 11, c# 11 required keyword, c# 11 new features, c# 11 features, required keyword, required, required keyword in c#, c# new features, Why I won’t need constructors anymore in C# 11
Id: 9CDgPgWF9IY
Channel Id: undefined
Length: 9min 39sec (579 seconds)
Published: Mon May 30 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.