The New Way of Parsing ANY Type in .NET

  Рет қаралды 67,305

Nick Chapsas

Nick Chapsas

Жыл бұрын

Check out my courses: dometrain.com
Become a Patreon and get source code access: / nickchapsas
Hello, everybody, I'm Nick, and in this video, I will show you a really cool feature that was added in .NET 7 that allows you to parse strings into any type with a consistent interface and programming model.
Workshops: bit.ly/nickworkshops
Don't forget to comment, like and subscribe :)
Social Media:
Follow me on GitHub: bit.ly/ChapsasGitHub
Follow me on Twitter: bit.ly/ChapsasTwitter
Connect on LinkedIn: bit.ly/ChapsasLinkedIn
Keep coding merch: keepcoding.shop
#csharp #dotnet

Пікірлер: 116
@michielvanhimbeeck6206
@michielvanhimbeeck6206 Жыл бұрын
I love how random your numbers are
@MarkCastle
@MarkCastle Жыл бұрын
Not watched it yet…. 69 by any chance? 😂
@dmytrk
@dmytrk Жыл бұрын
​@@MarkCastleand then splits it with comma "6,9"😂
@kriscavitt3479
@kriscavitt3479 Жыл бұрын
And 4,20
@marsgal42
@marsgal42 Жыл бұрын
The only numbers that matter are 13 and 42. 😛
@jonathanbout
@jonathanbout Жыл бұрын
The like count of this comment is one of them
@carldaniel6510
@carldaniel6510 Жыл бұрын
All of the new "generic math" interfaces are awesome - long overdue. But please - DON'T implement TryParse as a call to Parse inside a try-catch. One of the key benefits of TryParse is that it does not throw - even internally. Implement Point2d.TryParse as two calls to int.TryParse and return true only if both succeed - no throw/catch inside.
@Sindrijo
@Sindrijo Жыл бұрын
Agree, only implement TryParse with other TryParse. I usually also implement the Parse with the TryParse version of the same type.
@nickpolyderopoulos3491
@nickpolyderopoulos3491 Жыл бұрын
I am pretty sure Nick did not do Try Parse as you propose for simplicity reasons. It is not a best practices video.
@amirhosseinahmadi3706
@amirhosseinahmadi3706 Жыл бұрын
"One of the key benefits of TryParse is that it does not throw - even internally" Could you explain what how that's a benefit? Genuine question. Wouldn't it be functionally the same?
@sszzt
@sszzt Жыл бұрын
​@@amirhosseinahmadi3706 It is just for performance reasons. Returning false is as fast as you can get. Throwing an exception requires much more processing and memory allocations. Of course you have to weigh up whether it's worth optimising your method for your particular use case. But stick to what @Sindrijo has said and 99% of the time it will be the right decision.
@dennycrane2938
@dennycrane2938 Жыл бұрын
I was actually going to comment the same. I do it the other way around. I do the validations during the parse implementation and just short-circuit if anything is wrong and return false. Then Parse just calls that and throws if it fails.
@woocaschnowak
@woocaschnowak Жыл бұрын
When MS said they will add static interface members, I thought, they lost they mind. But now, that they provided such use cases, I'm changing my opinion :-)
@SvdSinner
@SvdSinner Жыл бұрын
This was a great blend of useful right now, interesting and showing future functionality. Kudos!
@quranthecompanion4528
@quranthecompanion4528 Жыл бұрын
we used to use Convert.ChangeType() for primitive data types. now we are changing the implementation to IParsable. its pretty neat.
@zoran123456
@zoran123456 Жыл бұрын
Understood everything up to 2:00. After that my brain went coo-coo!
@davidtaylor3771
@davidtaylor3771 Жыл бұрын
That stuff at the end was interesting because it showed a relatively easy way to do parsing in .net 8 without allocating anything other than the initial input lines. Imagine you were parsing a large CSV file where you always had 10 fields per line separated by comma. You can split that string into 10 fields and do stuff with the fields (like converting them to types like DateTime, int, double, etc) all without any additional string allocations. Nothing extra for the garbage collector to deal with. And the code was relatively readable and very secure. It would perform like lightning if you were parsing huge files.
@dvldev
@dvldev Жыл бұрын
Great! I am watching your video since last month. I have completly changed the way of how I write code. Thank you very much Nick.
@mariocamspam72
@mariocamspam72 Жыл бұрын
10:11 We're circling right back to pre-2000s C, haha
@davidtaylor3771
@davidtaylor3771 Жыл бұрын
No we are not, that's the point. We can do what you did in pre-2000s C, but in a modern secure type safe way with extremely readable code.
@cn-ml
@cn-ml Жыл бұрын
I really like the new Span splitting method, makes me feel in control of my memory. Also i totally love the new abstract interfaces, especially the IParsable, but I probably used INumber a lot more already.
@alexanderkvenvolden4067
@alexanderkvenvolden4067 Жыл бұрын
I love these new interfaces and the ability to have static interface methods, and use them IF the code base I'm working on is using the supported runtime.
@ntohl
@ntohl Жыл бұрын
Nice. In the ReadOnlySpan TryParse example. Shouldn't it return false instead of throwing ArgumentOutOfRangeException?
@nickchapsas
@nickchapsas Жыл бұрын
Yes you are right. I copied and pasted it from the Parse method so I totally forgot. Thanks for bringing this up
@haveaniceday4585
@haveaniceday4585 Жыл бұрын
Hey Nick, I saw you yesterday at Techorama. Thanks for the great talk!
@nickchapsas
@nickchapsas Жыл бұрын
Hey! ❤
@craigmunday3707
@craigmunday3707 2 ай бұрын
Very useful, thanks.
@OXARA
@OXARA Жыл бұрын
Good practice for csv line parsing
@drndn
@drndn 10 ай бұрын
I appreciate how you use a similar naming scheme to me, semantic-type-as-physical-type, eg FooAsText, FooAsInt etc.
@gneto.r
@gneto.r Жыл бұрын
Okay maybe I need to rewatch this later after a cup of coffee or two... looks like something useful to fully understand.
@TheAaandyyy
@TheAaandyyy Жыл бұрын
Great video. One thing that I was missing was to introduce the [NotNullWhen(true)], but maybe that is the separate topic itself
@AEF23C20
@AEF23C20 Жыл бұрын
two points used from some buffer [read i/o etc], and this buffer easy to imaging as array, and then change type of this array, and array type - desired type of structure array
@josephmoreno9733
@josephmoreno9733 Жыл бұрын
Static abstract methods port interfaces to types beyond the instance of those types. And that is really impressive. With that, the need for the use of reflection is reduced as much as possible.
@diadetediotedio6918
@diadetediotedio6918 Жыл бұрын
I very much like this feature, I would rather prefer the "shapes" approach but this is really very very very userful
@Yogs01180
@Yogs01180 Жыл бұрын
This can be cool ! Thanks ! ❤❤
@timseguine2
@timseguine2 Жыл бұрын
Not sure If I am going to use this technique, but I did learn about static abstract members now, which I didn't know about before.
@Naton
@Naton 11 ай бұрын
i use 'is' syntax and return an Option
@hanspetervollhorst1
@hanspetervollhorst1 Жыл бұрын
The span part is imo way more interesting than the interface and abstract static stuff
@T___Brown
@T___Brown Жыл бұрын
Question: is there something built in that can do fixed width? Currently i have to do a lot of specific span reads but if there was a range like you had that i could split based upon multiple indexes then that might be better
@T___Brown
@T___Brown Жыл бұрын
Awesome ty. This is waaaaay overdue. Doing this with generics is painful and not really possible
@JVimes
@JVimes Жыл бұрын
Favorite thumbnail
@GekoPoiss
@GekoPoiss Жыл бұрын
I'd recommend using a parser combinator library instead. It's IMO by far the easiest and most intuitive way to parse any object, especially considering parsers are monadic, which means you can use LINQ to easily compose parsers.
@DaliborHomola
@DaliborHomola 11 ай бұрын
Does somebody know if Complete Nick Chapsas Course Bundle will get all future courses? Please ☺ I'm thinking of buying it...
@theloststarbounder
@theloststarbounder Жыл бұрын
Nice
@gunnarliljas8459
@gunnarliljas8459 Жыл бұрын
Nice. Wouldn't call that extension "Parse" though, since the string isn't parsing, it's being parsed.
@nickchapsas
@nickchapsas Жыл бұрын
ParseTo would be a better one
@parlor3115
@parlor3115 Жыл бұрын
@@nickchapsas Or ParseMe
@user-tk2jy8xr8b
@user-tk2jy8xr8b Жыл бұрын
Beparsed (similarly to "begone")
@diadetediotedio6918
@diadetediotedio6918 Жыл бұрын
"Parse" makes sense for me, it's like -> parse as
@bartekm3878
@bartekm3878 Жыл бұрын
Heck yeah! The puns are back! ❤
@waynehamberg9248
@waynehamberg9248 9 ай бұрын
Hey Nick. What software are you using to do your screen presentation with your picture as part of the production?
@oralitical
@oralitical 4 ай бұрын
I didn't see a reply, but it looks like the type of thing that can be done in OBS Studio.
@efrenb5
@efrenb5 Жыл бұрын
Does System.Text.Json automatically use the IParseable interface? I'd love to get rid of som of the JsonConverters I've had to build.
@ondrejjakub
@ondrejjakub Жыл бұрын
nice
@deathpax
@deathpax Жыл бұрын
Is there anything that would be similarly performant for binary parsing?
@TheMCPepo
@TheMCPepo Жыл бұрын
Hi Nick! I'm having kind of a hard time understanding where would you use in real life delegates and events, as we as covariance and contravariance. Do you have anything on your videos on this? Great video as always!
@xybersurfer
@xybersurfer Жыл бұрын
abstract static members. that's nice. it was about time to be honest
@F1nalspace
@F1nalspace Жыл бұрын
Awesome! This is actually pretty useful but comes far too late, because i already implemented something similar in our production code bases - implemented in hundres of classes/structs. But when we switch to .NET 7 (we are currently at NET 6), we may drop to this method instead. Wait, what is the '!' operator for s!.Split(',');? WIll this throw an exception when s is null?
@theMagos
@theMagos Жыл бұрын
You're telling the compiler: "s is never null, trust me, so stop giving me those null reference warnings..."
@theonlywallrus
@theonlywallrus Жыл бұрын
just curious, what are you beginning to type when you are setting a default parameter? You start typing `format = by`, but then you backspace and type null. You did it twice haha
@Ristogod
@Ristogod Жыл бұрын
How does the IFormatProvider play into your custom parse logic?
@phizc
@phizc Жыл бұрын
In the case of ints it wouldn't, but for doubles, some locals use period and some use comma as the decimal separator. Not to mention all the different date/time formats.
@arodrig10
@arodrig10 Жыл бұрын
Nice "random" number you chose 😳🤣
@mynameisshadywhat
@mynameisshadywhat Жыл бұрын
Just parsing the time
@DM-rc8sp
@DM-rc8sp Жыл бұрын
@phizc
@phizc Жыл бұрын
I knew about this, but I didn't know the out parameter of the TryParse methods came after the IFormatProvider parameter. That's pretty dumb IMO, since then you can't have a default null vlue, and the user needs to pass null.. int.TryParse( "20", null, out var value ) vs Int.TryParse( "20", out var value )
@IMarvinTPA
@IMarvinTPA Жыл бұрын
I really wish there were a GetParse(inputString, optional DefaultValue ) where failures return the default value.
@tekozlofiu
@tekozlofiu Жыл бұрын
I'm probably missing something, but what prevents you to create an extension method just like that?
@IMarvinTPA
@IMarvinTPA Жыл бұрын
@@tekozlofiu I actually already have, but I cannot share them.
@EdKolis
@EdKolis Жыл бұрын
I wonder why it's not spelled IParseable like ICloneable?
@kylekeenan3485
@kylekeenan3485 10 ай бұрын
Just a random number Nick 😉
@Eric19850704
@Eric19850704 Жыл бұрын
Thanks!
@MaksMikhnevych
@MaksMikhnevych Жыл бұрын
Why record, not a strict? What are the advantages and when to use which?
@ciach0_
@ciach0_ Жыл бұрын
records are immutable
@theMagos
@theMagos Жыл бұрын
Record is reference type, like class.
@phizc
@phizc Жыл бұрын
He could have used record struct too.. It's just an example for the parser, so he probably didn't bother. Brevity.
@tmcheah
@tmcheah Жыл бұрын
"Random number" Casually type in "69" 😂😂
@johnnm3207
@johnnm3207 Жыл бұрын
Benchmarks?
@othmanekinane2769
@othmanekinane2769 Жыл бұрын
In 09:38, why not just do this ? int commaPos = s.IndexOf(','); int first = int.Parse(s.Slice(0, commaPos)); int second = int.Parse(s.Slice(commaPos + 1));
@davidtaylor3771
@davidtaylor3771 Жыл бұрын
Try the equivalent for parsing a CSV file with 10 fields and 10 commas per line. Then look again at Nick's code. Nick is just giving you a demo, but this stays elegant when you have 10 fields per line of text.
@Mmss2304
@Mmss2304 Жыл бұрын
Of course the number input used as an example is 69.
@zabustifu
@zabustifu Жыл бұрын
11:52: is it actually a good idea to throw an exception here? I'd just expect false to be returned here, and the result to be set to the default value. On the caller side, having to wrap a TryParse call within a try / catch sounds like it defeats the point of using TryParse in the first place.
@nickchapsas
@nickchapsas Жыл бұрын
I mentioned this in another reply but no you shouldn’t throw an exception. I did the stupid think of copying from the Parse method but you should return false there
@MudzNZ
@MudzNZ Жыл бұрын
Noice number
@mohamedfazrin4172
@mohamedfazrin4172 Жыл бұрын
When Nick says Random number it means always 69
@EdKolis
@EdKolis Жыл бұрын
6:04 hehe ass pan
@iGexogen
@iGexogen Жыл бұрын
About your example - why not just reuse Parse method from TryParse? And in general - is there such use case where logic in Parse and TryParse should be different? Any reason for ms developers to put it on me to implement both methods rather than implement only Parse?
@nickchapsas
@nickchapsas Жыл бұрын
There is a difference actually and I didn't properly show this. TryParse should now throw an exception. Instead it should return either true or false based on whether it could parse the input, so me returning argument exception is wrong there. I should just be returning false. It's just a common .NET pattern.
@iGexogen
@iGexogen Жыл бұрын
@@nickchapsas Here's general implementation of TryParse try { result = Parse(s, provider); return true; } catch { result = default; return false; } I think it's just boilerplate and it is place in single extension method for whole codebase, and only Parse method have to be implemented. Maybe you know use case where it makes any sense to implement both?
@user-tk2jy8xr8b
@user-tk2jy8xr8b Жыл бұрын
@@iGexogen better do it the opposite way: if TryParse returns false - throw
@diadetediotedio6918
@diadetediotedio6918 Жыл бұрын
@@iGexogen He already said the use case, not throwing exceptions. From where did you take this implementation you provided as a 'general implementation'? Because throwing exceptions has a huge cost, it probably is not implemented this way.
@nacuflavius9457
@nacuflavius9457 Жыл бұрын
why number 69? it's not a random number at all...hmm
@valera924
@valera924 Жыл бұрын
Totally random number, there is no any meaning in it :D
@phizc
@phizc Жыл бұрын
@@valera924 yup. Like (4,20), or 80085 that he also randomly chooses. I mean, it is random, but there's fewer options than a 4 sided die. 😆
@TampaCEO
@TampaCEO Жыл бұрын
You really should overload the method to allow the caller to pass a string, rather than requiring your calling methods to add the .AsSpan() every time. It greatly simplifies your code and does not requie an .AsSpan() function to be called throughout your application.
@kyrylovagabond9172
@kyrylovagabond9172 Жыл бұрын
"Just random number" 😅
@mateuszk4419
@mateuszk4419 Жыл бұрын
"random number"
@rdmon746
@rdmon746 Жыл бұрын
int.Paws? int.Pause? 😅
@cliffwakefield
@cliffwakefield Жыл бұрын
.NET 8 preview 5, really?
@swordblaster2596
@swordblaster2596 Жыл бұрын
Pretty marginal stuff IMO
@a-s733
@a-s733 Жыл бұрын
seems not realy necessary...
@AftercastGames
@AftercastGames Жыл бұрын
Meh. Kinda cool, but ultimately not terribly useful, because types rarely only have one string representation. If you want to create a different type for each representation, I guess this would work, but it just seems that writing a static parse function would be quicker and easier to understand. But we’ll see. Maybe I’m just getting old, but adding new language features every month or two seems a bit chaotic, I think. You can’t even start and finish a project before your code is already obsolete. 🙄
@liquidcode1704
@liquidcode1704 Жыл бұрын
bro you lost me 50 million "if we go into that"s ago
What is Span in C# and why you should be using it
15:15
Nick Chapsas
Рет қаралды 248 М.
Don't throw exceptions in C#. Do this instead
18:13
Nick Chapsas
Рет қаралды 249 М.
Sprinting with More and More Money
00:29
MrBeast
Рет қаралды 170 МЛН
Универ. 13 лет спустя - ВСЕ СЕРИИ ПОДРЯД
9:07:11
Комедии 2023
Рет қаралды 3,4 МЛН
He tried to save his parking spot, instant karma
00:28
Zach King
Рет қаралды 23 МЛН
NextJs E-commerce Admin Dashboard & Flutter E-commerce App
3:23
Code With ILias
Рет қаралды 280
The New Data Protection Features of .NET 8 (GDPR)
14:10
Nick Chapsas
Рет қаралды 48 М.
how Google writes gorgeous C++
7:40
Low Level Learning
Рет қаралды 784 М.
Solving One of the Biggest Array Issues in C#
10:36
Nick Chapsas
Рет қаралды 34 М.
The Right Way to Check for Null in C#
9:35
Nick Chapsas
Рет қаралды 94 М.
What’s the Result Type Everyone Is Using in .NET?
14:47
Nick Chapsas
Рет қаралды 101 М.
C# TryParse - Converting strings in other data types
7:28
tutorialsEU - C#
Рет қаралды 3,5 М.
The weirdest way to loop in C# is also the fastest
12:55
Nick Chapsas
Рет қаралды 247 М.
Swagger is Going Away in .NET 9!
10:48
Nick Chapsas
Рет қаралды 84 М.
Functional Parsing - Computerphile
22:46
Computerphile
Рет қаралды 133 М.
Sprinting with More and More Money
00:29
MrBeast
Рет қаралды 170 МЛН