The List Best Practice That .NET 8 Makes Possible

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 i.net 8 Microsoft finally makes it possible for us to optimize for memory in one of the most common scenarios when dealing with collections if you like that with content and you want to see more make sure you subscribe ring the sub notification Bell and for more training check out nickjavasis.com but before I move on I'm extremely happy to announce that I just launched my brand new course from Zero to Hero rest apis in.net it is the only cost you need to learn how to build elegant rest apis in.net and this effectively the combination of what I learned in the past six years of my career building rest apis for millions of customers and some of the biggest companies in the world it starts from the theory and the most basic features of rest apis and it goes into some of the most advanced ones like building sdks for your consumers to consume your rest API programmatically and even contains a migration guide to move from controller based apis which this course is using into minimal apis to celebrate the launch I'm offering the first 200 of you a 15 percent discount codes so if you want to do what thousands of you have done already check that link in the description and apply discount code rest15 for that discount now back to the video all right let me show what I have here and what I mean by that so I have a simple console application doesn't really have anything here but I have this user service class and this one has a record of a type user and then a user service with a single method get users now this one is sort of a pseudo code version of what you might have in your code and the idea is that in many methods on your projects that return some form of collection whether that's a list a collection an innumerable an array anything you might have some form of validation or some code to a database or something in general before you eventually return that collection with the data in it and if validation fails or if the call to database returns nothing or even if it fails what are you going to return here many people choose to do if you know validation fails return null however over in my opinion this is very much a bad practice because this can lead to null reference exceptions being thrown typically consuming that method later so you have to have if checks everywhere however what's the most likely thing you're going to do with a collection like this you're going to use Link to maybe select or filter or do something like that or iterate so you will actually write safer code if you just return an empty collection here so do something like this new list of user however this can also be very problematic and people who Define returning null say that if you do this you're gonna return an empty list which will still allocate memory every time because there is no single empty list you can return in the form of a Singleton so you're gonna be allocating memory every single time you're returning it now some types have already solved this problem for example if I go ahead and I change this to a numerable type user then you could say enumerable dot empty type user and that will return the same empty instance of the numerable every single time and if this was an array then you could also say array dot empty and pass down the user type and then this will be the same thing it's not going to allocate any memory it's the same cast array type every single time however for read only collections read-only lists and also a very common scenario read only dictionaries we do not have that until now finally in.net 8 Microsoft adds ways to deal with this now this project is currently dot Net 7 so I'm going to go ahead and change it to use the latest Alpha of.net 8 and also do it here in the Cs project here we go and now we can use those new methods let's call the program.cs and see what those methods are so as we saw already before in a scenario like this you'd have to return a new list of type user however now all you need to say is read only collection dot empty and that is it that is going to be the exact same instance of the empty collection every single time preventing you from allocating that extra memory every time you invoke it this is now the same for I read only collection so if I had that then this is exactly the same oh and for those of you using uh the observable collections then those also got the exact same single empty instance treatment so if you're using those types read only observable collection dot empty is what you want to use but the biggest one for me the one I'm going to use by far the most is the dictionary so if you have an i dictionary of type int or user to get the users then previously you'd have to say new dictionary like this but now all you need to do is say read only dictionary.empty and that is it it's going to return the same empty dictionary now what does that mean in terms of performance for your code well let's add some benchmarks and see how much memory now we're gonna save every time by using using this approach of course I'm going to use benchmark.net to add my benchmarks let's go ahead and just add the nuget package and I'm going to bring in this benchmark.cs class now we're using a memory diagnosis because we want to know how much memory we allocate and these are the three main things we're testing for so I read only list which by the way can also be I list the reason why I don't like returning I list when I'm returning a read-only list is because it doesn't communicate intent this is supposed to be something you are consuming and you're not just adding back into because just to quickly show you if this was an eye list over here of type user and I returned read only collection empty then if I try to add a new user I still have the method because I list has the method but if I tried to run this then as you're going to see over here I'm gonna get an exception because the backing type is read only so even though this is I list this does not communicate intent you want to go have it on the list and then you're gonna get the compilation error because you truly can't use that and by the way if you want a video explaining when to use each type leave a comment down below let me know and I'll be happy to make that so I read only list here and I'm going to compare that to returning a new list of capacity zero then I also check the read-only collection which is a different interface and I'm returning read-only collection or new collection of user which is what you might do if you're returning a collection and then the I read only dictionary which again I can use I dictionary here but if I do that I don't communicate to the user the consuming user that you're not supposed to add things here this is just read only for your consumption so now with all of these things over here I'm gonna go to program.cs I'm gonna say return at the top and Benchmark Runner dot run and I'm gonna pass down the benchmarks type and I'm gonna quickly change this to release mode and run and see how all the bands must compare all right so results are back let's see what we have here so let's go Pair by pair so read-only list new 0.6 nanoseconds and then the old version 4.1 which is fine but 32 bytes of memory for an empty collection that you're wasting every time the thing doesn't return any data same with the collection 0.6 but 7.8 even slower if you do a new collection in your code and even more memory and then the dictionary 0.6 so obviously all of them are equally blazing fast and 6.8 and 80 bytes for nothing so this was the biggest counter argument to people saying don't return empty list or empty collection or empty whatever just return knowledge You're Gonna Save memory and in dotnet 8 that will no longer be true so when that is out keep an eye out for that and start using it into your code it is going to lead to better code returning an empty collection that now it's cached so it's even more performance and compared to just returning null and in my opinion and that's just how I do things even with the memory allocated I'm still returning a new empty list or a new empty dictionary because it will lead to better and safer code one person forgets to add the null trick and the whole thing blows up so that's my advice to you on this subject but what do you think do you prefer returning null for empty collections or just a new empty collection leave a comment down below and let me know well that's all I have for you for this video thank you very much for watching especially next to my patreon to make it videos possible if you want to support me as well you're going to find the link description down below leave a like if you like this video subscribe like this in the Bell as well and I'll see you in the next video keep coding
Info
Channel: Nick Chapsas
Views: 38,862
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, .net 8, return null, return empty collection, array.empty, enumerable.empty, list.emty, readonly.empty, The Enumerable Best Practice That .NET 8 Made Possible
Id: xIA3t2Je3uQ
Channel Id: undefined
Length: 8min 40sec (520 seconds)
Published: Mon Feb 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.