The Magic of Breaking Down Your Types in C#

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 a fairly unknown and underrepresented C feature called object deconstruction that I think is really really cool now as a feature it is natively supported by many of the types in net already but you can actually supercharge your own types by introducing it and making the developer experience hopefully better so in this video I'm going to explain what it is how it works where you get it natively and trust me there's a couple of places you might not expect it and way way more if you like of content you want to see more make sure you subscribe for more training check out my courses on do train.com okay so what is objective construction well let's go in this console application and basically show you what I'm going to do is I'm going to create a key value pair object just a simple object so I'm going to say key value pair in case you don't know what that is it is well as the name implies a key value pair the key is the string in this case the value is the integer so if I wanted to initialize this with just one item I could have said for examp example Nick over here and then just some number over here and I can then go ahead and use that and I can get the key and I can get the value now that is cool but I can also do the following I don't have to use it as a key value pair I can also use a truple and say key and value and then I have these two individual parameters as you can see the string over here and the number over here the value individually now of course if I don't need any of those I can just discard them by using the discard Operator just a single underscore will basically remove it from the equation so you can't really use it but you can still use the key itself this is very useful especially when you're iterating key value PA through a list or also for example if you have a dictionary let's say new dictionary over here again string and number if you're iterating that you might have noticed that you can do the following you can say for each for example and those values are key value pairs so you can say key and value I'm going to quickly comment this out and this in my opinion is better than getting the individual entry of the dictionary because it just gives you direct access to those items so how is that done well this is done through a process called object deconstruction now if we go all the way back to this first example with the key value pair you might have seen that as I'm clicking key value pair and I say dot there's a method here called deconstruct so I'm not going to show you exactly what's in there yet but as you can tell by here this de construction feature is enabled through a method now that means that I can create that method for any type for example I can go here and say class Point 2D so that is a point in 2D space and I'm going to represent that with integers so I'm going to say int X and in y this is just a value or a point in the space so value X and value y gives is the point now I could do the following I could say point over here equals new point and then give it again a couple of values so one and two if I do that then I can go ahead and get each individual type by saying dox Andy but I can also go in that class and I can say public void deconstruct and yes the name matters it has to be called deconstruct This is done through typing effectively and then I want to have the parameters I want to make the constructable or this type the constructable into as out parameters so out in X and out int Y and if I do that then I can go ahead and set those out parameters to any value I want in this case it's going to be the property values of X and Y but you can also do calculations if you want you can do whatever you want in here by doing that now I can go to point and I can say X and Y and immediately deconstruct that type using this method now here's something very very interesting imagine that this is not a class but it is actually a record so I say record Point 2D and then I have int X and int y the interesting thing about that and I have to change this a bit so I'm going to say again couple of random numbers as you can can see I didn't change anything I didn't add a deconstructor myself but what's happening is that because this is a record records automatically if we go over here into the IL viewer and move myself out of the way and quickly build this project you will see that a record because the record is basically compiled into lowlevel cop from high level cop if I say lowlevel cop you will find this deconstruct where is it it should be all the way down this deconstruct compiler generated deconstructor that allows this to happen so records get that for their properties by default classes don't you can add it yourself now this is really really cool but it actually gets cooler because what if there's a type that you don't own for example let's go ahead and assume that I want to add a deconstructor to something like an exception let's say I have the exception class here can say new exception and then I can have like a stack trace and a message and whatever but for some reason when I'm not saying this is a good practice don't do that I just want an example of a type we don't really own directly I can go here and I can say I want this to be the message of the exception and I want this to be the stack trace of the exception how do I do this is it even possible and the answer is yes you can actually do this what you have to do is create a new class and I'm going to say exception extensions over here and I'm going to change that to a static class because I'm going to have an extension method here and extension method requires a static class so I'm going to go here and say public static void deconstruct the name is super important and then I'm going to say this exception exception so this is an extension of the exception class and then I'm going to say out string message and out string which is nullable stack Trace because the stack Trace is nullable and then I can take that message and I can say message equals exception. message and then stack Trace equals exception do stack trace and by doing that what what happens here I can import that extension method through using that name space and now my message is here and my stack Trace is here and I can access them directly without having to use the full object now of course with this feature you have to be careful for example this is not great idea because it's not very obvious what you're deconstructing into but whenever something is obvious and it makes sense in the context of what you're doing I highly recommend you consider deconstruction it can make your life way easier like it does it with this key value native deconstruction that in this case looks like this let's see if we can actually step into that here you go it is a key and a value simply Constructor and you should know that tons of C native types actually support this BCL type for example if you say date time over here we have a new date time well date time has multiple deconstructors which by the way you can have for example I can say that this is date and time if I'm remembering correctly yes this de construct into date only and time only so you can break it down to many uh different types or I think you can say year month and day if I'm not wrong so int int int if we go in here as you're going to see is year month and day so you have a couple of options on things that do make sense I don't think you can deconstruct any further at to have hour minutes and seconds but let's try it nope it is not possible but this is something you can actually add a deconstructor yourself if you really want to I think this is a bit too much I don't like that you have so many types that it's getting so long so I personally don't recommend it but two or three it does really make sense but now want to know from you what do you think about this feature and please leave a comment down below letting me know what Native BCL types you think that can have a deconstructor and what should that deconstruct into for example daytime here yeah that makes sense key value pair here yeah that makes sense but what would you add if you could change the BCL maybe the net team sees that video and they add in net 9 well that's all I had for you for video thank you very much for watching and as always keep coding
Info
Channel: Nick Chapsas
Views: 62,705
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# deconstruction, records, .net records, c# records, .net deconstruction, type deconstruction, object deconstruction
Id: 55_E5m-YBpU
Channel Id: undefined
Length: 8min 58sec (538 seconds)
Published: Thu Jan 25 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.