Quick word on LINQ
So today’s post is going to be focused around LINQ which is an acronym for Language INtegrated Query. For those who are unaware and would be interested in learning more about LINQ, I invite you to go on this page. In a few words, LINQ is the name for a set of technologies based on the integration of query capabilities directly into the C# language.
In the past, I have had some questions and I’m leveraging my blog to share my experience on the topic and hope to make you better developers by using the experience that I’m sharing. So, with no further ado, let’s dive into the tech question of the day!
#4: Should I use ToArray() / ToList() in LINQ queries?
So, when we use the LINQ, every extension method returns an IEnumerable and they can be chained together to let you, the developer, do whatever it is that you have to do, like say finding the name of author in the database which sold over 100 books in the year 2018 for instance. The power of LINQ can found in its deferred expressions. What’s a deferred expression? Good question! When you’re writing your LINQ expression such as
Your expression doesn’t go through the entire collection until told otherwise. What happens behind the scene is that until forced to moved the enumerator to the next index, LINQ is going to wait. What good does that bring you might ask? It also means that you won’t allocate useful memory until there’s no choice but to allocate it and do what it is that you have to do with it.
Back to Should I use ToArray/ToList in LINQ queries. The best answer that I can give you is that it depends on the context, but I frown upon the usage when deemed unnecessary. Why do I frown upon that? Because it allocates memory for your entire collection that will be generated by your LINQ expression. Using those methods have a performance hit on your entire expression of a size of O(n). Depending on the context, if I use one of those endpoints in the LINQ API, I’d use ToArray over ToList for purposes such as iterating through the resulting collection. ToArray has a bit less of a performance impact on your code. When your collection is too big, it must grow behind the scene an array of a size big enough to contain all your data and then returns said array. In the case of ToList, again, we’re growing an array, but once the array is set, we’re initializing a list object with the resulting array which means that you’re allocating X.Y Gb memory twice from your query.
As I mentioned earlier, it all depends on your context. Maybe after having your resulting collection, you’ll be in the need of a list for a specific endeavor. When I’m using LINQ, most of the time, I’m making sure that my API endpoints return IEnumerable<T> to let the users of my code do whatever they want with them. If you need to create a dictionary or a set from my method, the returning value lets you do that since IEnumerable is the common interface that collections point to (usually).
Thank you for your time,