TryGetNonEnumeratedCount Method In C# 10

Introduction

In today’s article we will take a look at a new feature that has been introduced in C# 10. This is the LINQ method to see if the enumeration we are using can provide the count without having to iterate through all elements in the sequence. So, let us begin.

Why and when to use the TryGetNonEnumeratedCount method

We know that when we call the count method on a IEnumerable<T> it is first checked to see if it can be casted to a ICollection<T>. If yes, then the count is provided immediately. If not, then in order to provide the count all the elements have to be looped through. This can be a performance issue, especially where we are reading a long list of elements. The “TryGetNonEnumeratedCount” method will return a true or false depending upon if the count can be returned without iterating the list of items. The actual count is provided in the out variable. Let us see this in action.

Creating a console application to test the TryGetNonEnumeratedCount method

We will start by creating a C# console application in Visual Studio 2022 community edition as below,

Using the TryGetNonEnumeratedCount method in Csharp

Using the TryGetNonEnumeratedCount method in Csharp

Using the TryGetNonEnumeratedCount method in Csharp

Using the TryGetNonEnumeratedCount method in Csharp

Using the TryGetNonEnumeratedCount method in Csharp

Update code in the Program.cs file as below,

using System.Collections;
IEnumerable<int> arrayOne = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var canGetCountDirectly = arrayOne.TryGetNonEnumeratedCount(out int theCount);
Console.WriteLine($ "Array One count can be returned directly = {canGetCountDirectly}");
Console.WriteLine($ "The count for Array One = {theCount}");
List<int> listOne = new List<int> { 1, 2, 3, 4, 5 };
canGetCountDirectly = listOne.TryGetNonEnumeratedCount(out int theCountList);
Console.WriteLine($ "List One count can be returned directly = {canGetCountDirectly}");
Console.WriteLine($ "The count for List One = {theCountList}");
MyItems < int > customOne = new MyItems < int > ();
canGetCountDirectly = customOne.TryGetNonEnumeratedCount(out int theCountCustom);
Console.WriteLine($ "Custom One count can be returned directly = {canGetCountDirectly}");
Console.WriteLine($ "The count for Custom One = {theCountCustom}");
class MyItems < T > : IEnumerable < T > {
    public IEnumerator < T > GetEnumerator() {
        throw new NotImplementedException();
    }
    IEnumerator IEnumerable.GetEnumerator() {
        return GetEnumerator();
    }
}

When we run the application, we see the below,

Using the TryGetNonEnumeratedCount method in Csharp

Here we see that in the first and second case we can get the count without iterating over the elements and hence the process will be very fast. However, in the third case, we would need to iterate over all elements to get the count.

Summary

In this article, we took a look at using the “TryGetNonEnumeratedCount” method in C# 10. We discussed the advantages of using this method and when it should be used. Although this might seem like a small feature, it does let us improve the performance of our application. A possible use case could be to return the count and see what method our collection is using to return the count. If it is iterating over all the elements we can work to change this to a ICollection<T> which does not iterate over all items and significantly improve the performance of our application. Happy coding!


Similar Articles