The New Way of Parsing ANY Type in .NET

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody I'm Nick and in this video I'm going to show you how you can make any c-sharp type in your dotnet code base parsable now this was technically possible before.net 7 but with the release of.net 7 and C sharp 11 this just completely changed and is made way more consistent and way easier as well it's something I've been using in my own code base for the past eight months now and I want to show you what I'm doing why I'm doing it and explain why it's a better solution than what we had before if you liked our content and you want to see more make sure you subscribe but in this notification Bell and for more training check out my courses on dumbtrain.com alright I'll be sure to have here I have a simply a.net 8 console application I'm on.net 8 preview 5 as of the release of this video but all you need is dot Net 7 for this to actually work and see shop 11 of course now let me show you what this is about fundamentally we have here some a number as text and this ultimately is about converting some text to some type and pausing that text into some type so for example I might have just around random number here and over here I might want to pause that number as an integer what I might do in pre.net seven times is int dot pause and just pause that number and then I can say console.writeline number as end and if I was to run this you're going to see that value printed in the console and that's it now if you wanted that number to be represented as a double because it can be passed as a w you can say pause and you can have that same thing over here we can copy this here just run it same idea the same value is printed just as a double this time now what I'm going to do is actually downgrade this project to dot net 6 to begin with so net 6 goes here and Dot net 6 goes here as well here we go so not.net8 nothing preview no nothing and I want to go into the pause method to see exactly what's really behind that method so as you can see it is just a static method called pause in the in32.cs class which is where where the integer is located in those times anyway now as you can see the in 32 type implements a few interfaces I comparable convertible as one formatable and a few other things but it does not as of the release of that version Implement anything specific to the pausing so as far as this pause method is concerned it is just a static method in that class and by the way you also have the try pause variant this is still falling under the same category and same idea we're just going to use pause in this video but everything we do for pause is applicable for tripass as well now if I upgrade this project to use the.net 7 SDK and Net 7 in the properties as well so now it is going to be a DOT Net 7 project what I'm going to do is just quickly rebuild so the Right Packages are pulled and I'm gonna go into that same pause method and now take a look at this even though this is still a static int pause method if I scroll all the way up you're gonna see that this is now changed previously if if you remember we had the flag saying generic math enabled this is how the static abstract members was called before it was released officially in C sharp 11 as an out of preview feature but take a look at what's happening here now on top of everything else we have some other interfaces like I sign number I mean max value and I binary integer if we go into this I binary integer we're going to another interface which implements the I binary number which if I go into that implements the I number which if we go into that implements the number of Base which if we go into that implements I span possible which if we go into that Implement I possible and now because we have static abstract members on interfaces this pause and try pass methods are actually static members of that interface and end is not unique in having this value in fact good long double all of those things that could be paused now are rooted in this interface is with this static abstract member why is that important well it's important because now all of them have the same sort of interface and the way they act but it's also important because now you can do something like this I can go ahead and I can make it public static possible extensions class if I wanted to and like I said I want a public static method here that returns generic T called pause and then I can use T here and then the input value is an extension on string which is the same as any pass method and then we have the input here and then we also have the i formatable provider parameter which will be null by default which is also another element in these methods if I go comma here you're going to see that we accept this I format provider interface which is nullable now this method can actually have a where Clause where I say that where T is actually an i possible of type T So if I do that what I can say because of that new feature is T dot pi pause and then pause the input and the provider and that is it I'm gonna say format provider over here and now instead of having this int.pause or double dot pause or whatever both of them can have an extension method called pause and then I say pause this as an integer and because they do implement the I possible interface in their own types both double and int does that I can do the same here and say pause this as an integer and pause this as a double which to me just reads way better than having to do int dot whatever double dot whatever this generic approach is very neat very nice you can have the same if you use I span formatable if you want to use span as the input method which is also an option of course for that to be optimal you have to change this to a read only span of type characters so you're gonna have something like this but if you want to do that then the way you're processing those has to match that so you might have say our Spanish here and so on I'm gonna revert it back to string because string is easier to work with and it's something everyone understands but if you want to work with fan that's also something you can do now to put everything into perspective to explain why that is so cool imagine the following I'm gonna go ahead and create a new record and we're going to call that point to the point in 2D space and I'm going to have an X and A Y axis so I'm going to say int X and int y over here now if I just leave this as it is I'm gonna have to instantiate this object with a Constructor however what I can do is say that now this is of I possible of type points to D its own type and the moment I do that I have to implement the two missing members the pause method and the try pause method now all you need to do to use these methods is actually take that string and write a method that converts that string to that type a new instantiation of 0.2d I'm gonna just change this to be null by default because we might not want to provide it and we provide a better experience by just sending it to null by default so you don't have to explicitly return null on the front end and now since I have that all I need to say is split text equals s dot split and we're going to split on a comma so the input we want to have is something like this for example and we're going to return that into a point 2D so we're going to say split text and then we split on that and then x equals split text the first value and we're going to say int dot pause of course we could just leverage what we just did and say pause to an integer which in my opinion looks pretty nice and pretty neat and then we can say return point to D and X and Y is back and of course I have to say new here and that is it so now what's going to happen is if someone says that the input that I have here number of text is these two parameters comma separated I'm going to say that points to D equals number as text pause and because point to D does implement that interface case it is going to allow me to do that now it doesn't like this because this doesn't Implement I span possibly that's just the case because I did not roll back this change the moment I do this is now acceptable and if I do that and if I run this code base then as you can see the point is converted and it is passed from that comma separated string to that type now I could of course implement the tripass method in a very similar way I'm going to have a try and then a catch over here and then I'm gonna say that this is the original passing over here this is definitely not null this has to be result equals new and then return true and then we can set a default value if we want for the point for the default version because this is not nullable so I can say something else and then return false and that is it now I have my tripods implementation and my pause implementation now I kind of did spoil that already but you can actually use spans as well here's the strings to be way more optimal with your processing because if you do split like this here I just create two more strings over here and an array allocation which is not optimal is three things that I could really do without so the way to prevent that and not have it at all is just change this I possible to I spun possibly which internally does already implement the eye possible so you're gonna have to implement both effectively so now I'm gonna go ahead and implement the missing members which is the pause method and the try pass method of the read-only span version and now I'm gonna do the exact same thing as I had before but the sort of API of spam changes a bit the things because now it's a read-only span of character so I don't really have this split method what I have instead is ranges and offsets but what I'm going to do in this case since I am on dotnet 8 or at least I have it installed is move to.net 8 and upgrade both my project and my solution to them and if I do that and I use the latest preview we actually have a new new split method coming now it's a very interesting interface you don't just pass in the parameter and that is it you actually need a destination span to write to so what I'm going to do here is say that I have a span of type range and that is my destination span and I'm going to stack allocate a span of type range with two parameters two spaces because I have X and I have y That's effectively what I'm doing here I'm going to say destination goes here coma and then the character I want to split on which is the same comma separated thing and now this bit with X and Y becomes a bit trickier to understand but I'm going to explain what's happening X now is installed pause of that original span and then the first range in the span of ranges so destination first and that is it this will give me the first value and the Y is the second one and that is it and just to show you how this works I'm gonna go here and say say that this is actually a span so I'm going to say point to D dot pause and I'm going to put the Span in here and now as you can see this will go into the span and if I go ahead and I run this this will be processed and if I just change the parameters as well this will also be processed absolutely fine they're just passed through the ranges and allocations are kept to a minimal amount in fact there is no Heap allocations here this is a stack allocated range we have displayed on the text which by the way we don't really need this because this tells you how many things you split on and then we have the int.pass that is also using a span over here so we really minimized all the allocations here this is very very efficient if you care about performance and you want to implement pausing you want to use the pause method that user read only spans over here and then the idea about the try pause method is effectively the same we just add an exception if you don't split on exactly two parameters and that is it and now you can add this implement relation to any of your types implemented in any way you want and pause any type especially if you're using things like minimal apis or any sort of API that has some input of some string and you want to convert it to a concrete type previously we had to do duct typing but now we can actually use the I possible interface and it's just way way better it's a very underappreciated feature and it's only when you need it that you really appreciate it but as someone who is doing text parsing quite a bit this is just such an amazing way to deal with this problem and solve it now whether you want to make your own extensions or not it's completely up to you but the fact that we now have this static abstract members makes it so easy to do and I absolutely love this approach but now I want to know from you how are you doing your pausing did you know about this interface and are you using it leave a comment down below and let me know well that's all advice for this video thank you very much for watching special effects to my patreons for making videos possible if you want to support me it's really going to find the link description down below leave a like if you like this video subscribe Bell as well and we'll see in the next video keep coding
Info
Channel: Nick Chapsas
Views: 64,815
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, iparsable, parse any type c#, parse any type .net, .net 7 parsable, virtual static members, parse anything c#, parse anything .net, The New Way of Parsing ANY Type in .NET
Id: lqbYURwM0bw
Channel Id: undefined
Length: 13min 3sec (783 seconds)
Published: Thu May 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.