How type deconstruction keeps your C# code clean

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody i'm nick and this video i'm going to talk about deconstructors in c shop now this is a feature that i honestly don't see being used enough it's a really handy feature that can really clean up your code but maybe people just don't know about it because it has a confusing name that can be mixed with another c sharp name that destructor and then you don't exactly know what it is how it works and how you can use it it's really truly awesome and in this video i'm gonna show you exactly how it works in all of its glory and how you can extend it to ultimately write clear code if you like that of content and you want to see more make sure you subscribe bring this notification bell to get related when i upload a new video so let's start from the basics i have just a blank project here and what i want to do first is i want to create a class here so i'm going to say public and yes this could be in its own file but i'm going to keep everything in one file for demo purposes so let's say i have a public class here a person and we're going to add a couple of properties here we're going to add the full name here and i'm going to change that to string and then let's add a date of birth which is date only so date of birth right nothing fancy let's also turn this into init only because i don't need to mutate those values and also let's make this like this so we have a person class now let's initialize a version of it so let's say person or even let's say nick equals new person and let's add my full name here and let's also add a date of birth so this is the new date on because that in c sharp six so let's say uh yeah you know just i won't put my actual lid of birth here but close enough so we have that here and then if we wanted to use that full name on the date of birth in our code we might you know do something like this where name.fullname and then date of birth right here you go and then you might actually use that to write line something so let's say the name is and then the actual name here so let's say a full name and then born in and then we're going to put the date so date of birth and let's just add this fella here to format it so if i was to use that and print it it would print the following right the name and then the month and the day that's the ones i'm selecting here the year we're dropping off now here's where the construction comes in these two properties because to my eyes this is like a data contract but it doesn't have to be but just as an example it would be nice if i could do this in one line you know if i could declare these two variables just instantly now someone might say hey nick why don't you just you know do this and then you don't have to deal with anything fancy well the reason why i do that is because from a use case to use case this might be like a good practice to do especially for clarity of code like if this or this were very long names very descriptive it could get convoluted if you mix them and match them in a template here like this one so for that reason we split it now what i can do actually to make this simpler for me is create a deconstructor so what is a deconstructor not to be confused by the way with a finalizer or a destructor which looks like this this is not that that's a destructor or finalizer what we're going to talk about is a deconstructor and what this is a public void method called the construct this is kind of a magic name and you can have out parameters so out and then string full name here and then out date only date of birth right so it looks like this let me move this away from my face and now i can say i can actually set those out parameters and say full name equals full name and then date of birth equals date of birth and what this does is it allows me to do the following i can say var full name coma var date of birth equals nick and i can delete that and now look all of a sudden those two lines went away i'm just pointing to that original thing and in fact if you want to be like oh why don't you just do this you know i can just comment this out and do it at the very beginning and i don't even have to have nick so you can but just for cloud here i'm going to split it just in this video so now you can do this and as you can see when i um debug my code i'm actually going to step into this method which you know that doesn't seem to be a method here this just looks like magic but when i step into here it takes me into that deconstruct method because it detects the name it knows what it is and it sets the variables these out parameters don't have to be in one for one match to your properties you can actually out anything you want this is completely up to you and as you can see here this will create these two variables date of birth and a full name and then we get the answer back in the console which is the same thing so that way we can write less and in fact the c sharp team has actually optimized this and they're like you don't need two vars here just write it like this var in the beginning and then deconstruction here and that's it if you don't need one of those parameters because you might not always own the deconstruct method you can discard them by using the discard operator which is just an underscore and now you don't have to worry about this um technically you could discard everything but i don't know why you would do that so yeah you know keep something now the interesting thing about this is that the constructors are actually part of many things in the language already let's look at the following i'm going to create a dictionary here and i'm going to say just new dictionary and i'm going to have a string and an end and this dictionary will have let's say one entry here again my name and then a number which i don't know what it's supposed to represent just a number and then if i get the first entry here which is dictionary.first this is a key value pair of those types so technically i could actually split this to a key and the value and as you can see this is acceptable code the deconstructor exists on the key value pair level and when i didn't want to do that and when i step over this as you can see key and value are set so deconstructors are already part of many things in the language now let's take this a step further because it would be nice for example you know we have this date of birth up here and let me just move it over here dob equals this right it would be nice if i could deconstruct the date only element the date only struck into its variables to use so it would be nice if i could say year month day equals dob but then that's you know this thing is part of the net library i can't really you know change anything about this this exists outside of my scope and it doesn't have a deconstructor so tough luck actually in case you missed it this was a setup for a feature called well you can actually have extension methods on that thing you can do the following you can say public static loss and say like deconstruct extensions or deconstruction extensions and then you can have a public static void method called again deconstruct right but now we can deconstruct externally on that date only struct so we can say this date only date only and we can out the parameters we want so first i want the year then i want the month and last i want the day or day and then the same way you did before year equals date only dot year and the same for the rest month for month and day for day and now magically that code works and yes indeed it is stepping in here let me just prove this to you real quick i'm gonna debug my code get all the way down here and then step into this call it takes me in the extension method deconstructor it allows me to set those variables and then i can use them anywhere i want just like that and this is very handy when you don't actually own the class or the struct you can do that lastly and this is sort of like the expected behavior to be honest but you can deconstruct tuples as well so you could have down here a tuple or something that returns a tuple which is string name and then in age and it's like get person details i don't know something like that and then you can return nick chapses and then the age and you can have something like this you can by default you would have the following you'd have person equals get person details and you can clearly do you know person.age or person dot name because it detects those values from the tuple but you could also deconstruct this by default into um nick name and then nick and this is again positional so string string age age and if you don't like something discard it this all works fine now the last thing i want to talk about in this video is the record type so record types were added in c sharp nine and you can have something like this you can have like a public record book and this book can have like a string um title and then maybe like a string i what's that id in the books oh isbn yeah sure isbn so you can have something like that right uh this should be capital i mean this can have proper case isbn fine so you can have that and what you can do in records by default is you can deconstruct so you can have title and isbn here simply by doing new book this is a title and then some id i don't know and then you get as you can see positionally the the title first and the isbn the reason why this happens by default is because internally record does implement the deconstruct method i really hope as i was talking about this feature that all the light bulbs in your brain were just lighting because this can be applied in so so many places and i think it's way better in most cases maybe not all of them but there is definitely a lot of cases where this can really make your code pop a lot more well that's all i have for you for this video thank you very much for watching special thanks to my patreons for making videos possible if you want to support me as well you're going to find a link in description down below leave a like if you like this video subscribe for more content like sharing the bell as well and i'll see you in the next video keep coding
Info
Channel: Nick Chapsas
Views: 26,031
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, clean code, dotnet, deconstruct, deconstruction c#, deconstruction .net, How type deconstruction helps keep your C# code clean, void deconstruct, out parameters
Id: iqbgXgrEB4M
Channel Id: undefined
Length: 10min 58sec (658 seconds)
Published: Thu Dec 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.