Adding a BETTER way to loop 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 show you how you can use some c-sharp techniques that very very few people know about to effectively Implement a brand new feature into the language without actually having to touch the compiler the feature we're going to be adding is coming from kotlin and even though very very simple is so so much powerful not only we're going to add it but we're going to make sure it's very very performant if you like our content and you want to see more make sure you subscribe or in the sub notification Bell and for more training check out Nick chapsters.com now just a quick reminder that we'll be running my two-day workshop introduction to effective testing in cshop and net in a few conferences this year for now these are NDC Minnesota and NDC London but also NDC Porto and Copenhagen will be announced so I'm gonna put links in the description if you want to speak with your manager or your company and make the trip to any of those locations and spend three days with me learning how you can properly test your c-sharp and dotnet code I hope to see you there now before I show you the C sharp code I want to show you what feature we're going to be adding and the fish we're going to be adding is the way way you can do for each Loops effectively in a range in kotlin so in kotlin you can do the following you can say for I in 0 to 10 and you can print or you can use this item in that Loop so if I run this this will print from 0 to 10 and this the equivalent of this would be 4 VAR I equals 0 I less or equals in this case to let's say 10 then I plus plus which as you can see is way too verbose and I'm really not a fan of this and therefore each alternative in C sharp would be something like this where you say for its VAR I in enumerable dot range but then you have to deal with the implications of what this will allocate and how slow it will be so I'm really not a fan of how c-sharp deals with creating a loop around just a known range of data and this could actually be a slice as well so if I want to say from 5 to 10 I can do that in kotlin so can I with the features I have now in C sharp and without touching the compiler implement this exact feature turns out I can and it's super super interesting so let's go here in the C shop and show you exactly the experience I want to give you so all I want to be able to say is for each VAR I in 0 2 10 in this case and this should print console the right line from 0 to 10. so console.writeline range from 0 to 10. now this might look impossible to you but watch what I can do I'm gonna go over here and create a new class called extensions I'm going to make this a static class and I'm going to add a single method here so public static custom and enumerator and I'm gonna call that get enumerator and then I'm going to extend what is effectively the type of this which is a range so I'm going to say extend the range over here and then return a new custom enumerator with a range passed down so now I'm going to create my enumerator now here's where this gets really really interesting because in many cases c-sharp supports duct typing and this is actually one of those cases so this will pass for a completely wide enumerator as long as it has some methods and properties in it so before I Implement anything here if I go back here you can see now that writer tells me oh you want to import that and once I do it detects it as a range and it just says that I'm missing some things for example it doesn't implement ineurable or ineurable of T nor has the suitable get enumerator method so now let's go ahead and implement this so first I'm going to turn this into a ref struct and I'm going to add the two methods this needs to be accepted by the compiler so first I'm going to of course add the a Constructor so this shuts up and then I'm going to add the two things which first is a current location on the enumerator so in current goes here and for now I'm just going to return 0 I'm going to implement this properly and the other thing is the Boolean method move next which basically tell it's hey there's more data to read go on and the moment I add those without implementing or extending anything this cold is now happy this code is now valid C sharp and actually I can even write this like this or like this if I run this nothing will happen but this is where the fun begins because now I can add the main two components I need which is a current pointer and then the end so I don't go out of bounds from the loop so I can say private and current and then private read-only end end and in here I'm going to say current equals range dot start dot value minus one because I'm going to plus plus one in here so I'm going to start from one value below and then I'm going to say end equals range dot end dot value and that's it now you might have detected a possible bug here we're gonna fix it in a second and then current will just point to current and then in move next all I'm going to say is current plus plus and then how do I know if I have more to move to the next thing in the iterator will say current less or equals of end and that's it and now look what happens I can go ahead and change this to zero and this works from zero to ten not only does it work but I can of course omit the zero in the beginning and have it implied so from the beginning with 0 to 10 or say give me a slice from five to ten and I can have a loop on that as you can see over here now the biggest problem with this as it is now is that I can technically say give me from let's say five to whatever and something like this just wouldn't work so I don't want to support it and the way to not support it is say something like if range dot end is from end then throw new not supported exception and once I do that if I run this this will throw an exception because you cannot go from something to basically I don't know endless it doesn't really make sense so now just to recap if I want five to ten something like this if I want zero to 10 something like this oh and by the way you could go completely crazy and do something like this and say for VAR I in 10. now the way this would work is you have to basically agree whether you're inclusive or exclusive of the edges so is it from zero to nine is it from one to ten is it from zero to ten that's completely up to you but you totally can or could do that if I turn this from a range to an end and say number then I could return a new Range here and say from zero to that number and then the code would work and if I let it run as you can see by default it goes from 0 to 10. so these are all ways you can extend it basically using duct typing which is secretly supported in these situations now you might be looking at this and saying okay Nick this looks awesome but I don't trust this performance because you're using a four inch Loop coming from a for Loop and how does that compare so let's see some numbers so I brought in the project benchmark.net to allow us to run the following benchmarks and normal for loop from 0 to 10 and our own extended loop from 0 to 10 and then I have a fake just empty method here just so this doesn't get removed during compile time and all I'm going to do is Mark this as release and then run it so let's wait and see how the two things compare so results are backing literally what we have here so as you can see the normal Loop 2.2 the extended Loop we wrote 6.8 which you know it's three times slower but there's no allocations we try to be very efficient and with that so in that context does these performance difference matter I would say no but you can make the argument well Nick for meat mattress well let's see what happens when you actually have bigger sizes of things you're iterating on I'm going to change this fixed value 10 here and bring a params attribute which will change the value of this thing in runtime basically three times one per value so what this will allow me to do is get an idea of how performance changes over time for bigger sizes so I'm gonna go ahead and run this benchmarks and see what they return now so results are back and let's see what we have here so as you can see now the performance difference as the sizes grow completely disappears so this is something are personally very confident to add anywhere in my code to replace this sad thing to something that's pretty pretty elegant in my opinion but what do you think would you use this do you know about those extension points leave a comment down below and let me know well that's all I had for you foreign
Info
Channel: Nick Chapsas
Views: 77,670
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, .net 7, new c# feature, custom c# feature, new c# features, Adding NEW C# features without changing the compiler, Adding a NEW feature in C# without changing the compiler, secret c# extension
Id: jmmz1cInNow
Channel Id: undefined
Length: 9min 24sec (564 seconds)
Published: Mon Oct 17 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.