Home Linq - TryGetNonEnumeratedCount
Post
Cancel

Linq - TryGetNonEnumeratedCount

.NET 6 introduces the new TryGetNonEnumeratedCount() extension method on System.Linq namespace to determine the number of elements in a sequence without forcing an enumeration.

bool TryGetNonEnumeratedCount<TSource>(this IEnumerable<TSource> source, out int count);


Count on more performance!


The usage is straightforward. The method will perform a series of type tests internally and return a boolean indicating if the count could be retrieved without forcing enumeration, then it will out the count.

To understand the advantages this new method brings to the light, we must know the difference between using <a The usage is straightforward. The method will perform a series of type tests internally and return a boolean indicating if the count could be retrieved without forcing enumeration, then it will out the count.

To understand the advantages this new method brings to the light, we must know the difference between using Count() method and Count property against collections.

You can read the links above for more detailed information on the official documentation, but put in simple words, if your collection implements ICollection, the Count property is available providing its number of elements. Quick and simple.

Otherwise, you have to use the Enumerable.Count() extension method on System.Linq to force an enumeration, which means it will iterate through each item to increment the returned counter, and it’s obviously less performative than Count.

As you can see, both methods have the same purpose but work differently behind the hood. With TryGetNonEnumeratedCount you will be able to know if you can count the elements in the cheapest way when it’s possible, and it’s up to you to handle it the best way when it isn’t.


Hands-On


In the previous post, I talked about the new Three-way Zip method, which in one of its arguments, I’ve used an array of integers representing the count of moons for each planet. The count was achieved by using a small class I named SmartCounter, and it uses the TryGetNonEnumeratedCount method.

Let’s do a simple benchmark with this interactive example on which you can choose the planet you want to count moons. Then, it counts the collection of moons by using both Count and Count() to show the performance differences.


Although the amount of data I used is minimal, you yet can see how Count performs better than Count().

Notice that an exception was thrown by the SmartCounter class when I typed an invalid planetID, which according to the application’s logic, it will use the PlanetFactory class to create an unknown planet. Its moons are coming from the BuildMoonsForUnknownPlanet() method that yield returns one element at a time, making a non-enumerated collection, and I threw the exception to illustrate the decision making in that case.


Final thoughts


Day to day, we face minor adjustments here and there to take advantage of intelligent features like that, and the overall application’s performance increases significantly. So keep exploring what the framework has been implementing and look for avoidable flaws in your code. It will be rewarding!


Check the project on GitHub



This post is licensed under CC BY 4.0 by the author.