Today's blog post is about efficient and effective ways to return an empty IEnumerable from a method.  Many developers need to be aware of the different options available.  Still, with the help of some benchmark testing and a deep dive into the appropriate methods, we will clear up any confusion and provide a comprehensive guide.  By the end of this post, you will have a solid understanding of the most efficient ways to return an empty IEnumerable from a method and be equipped to make informed decisions for your projects.

Intro

As per my experience,  there are three common ways to return an empty IEnumerable from a method:

1. new List<T>()

This creates a new instance of an empty List object, which implements the IEnumerable interface.  This method is so straightforward.

2. new List<T>(0)

This creates a new instance of a List object with an initial capacity of 0 (zero). This method is similar to new List ().

3. Enumerable.Empty<T>()

This method returns a singleton instance of an empty IEnumerable object.

Benchmarking

Let's talk about these methods behave in a benchmark test.  According to the results, we can deep-dive into the method's actual implementation.

Let's examine further by benchmarking the performance using Benchmark.Net.  For the tests, let's create the following class.

After benchmarking, I got the below results for memory allocation and execution time.

As you can see, though the StaticEmptyMethod is slightly faster, it also consumes non of the memory.  How does it happen?  It's time to investigate the reasons.

Why new List() bit slow than others

This method is straightforward but can result in unnecessary object creation, affecting performance, especially in situations where the empty collection is returned frequently.  As well, every time it allocates memory for the new List.

Additionally, it creates a list with an initial capacity of 0, but the capacity is automatically resized as elements are added to the list.  This means that the list will be resized multiple times as elements are added, which can affect performance.

Why new List(0) bit efficient than new List()

This method is similar to new List () but provides a more efficient way to create an empty list when you know the final size of the List will be 0.  This means that the list will not need to be resized as elements are added, resulting in more efficient performance.

Why does Enumerable.Empty() show well efficient than others

As you can see here, Empty() is just a static method returning Instance from EmptyEnumerable class…

going bit further....

Oh, look, just a static property of a 0-sized array!  So every time we call Enumerable.Empty<T>(); we get the same array.  So when we call Enumerable.Empty<T>() 100 times; again there will be just 1 array more in the memory instead of 100.  So there is not 100 memory allocations 100 times.  Wow, It is an awasome practice by Microsoft.

Also interesting code commit there:

// We have added some optimization in SZArrayHelper class to cache the enumerator of zero length arrays so
// the enumerator will be created once per type.
🤩
Well they cache it, singleton it, and returns one static instance.
⚠️
Interesting:
If you love to know how do Enumurables working behind the sence, please refer this sourcecode. It will give you the ground level experience on the .Net practices.

Also Enumerable.Empty() added consistency for the code.
Using Enumerable.Empty() makes it clear that the intention is to return an empty collection, rather than a collection with a specific number of elements. This can improve code readability and maintainability.

Also can ensure consistency throughout your codebase and make it easier to maintain a common pattern for creating empty collections.

My Thoughts and Conclusion

In terms of memory usage, ⚡️Enumerable.Empty()⚡️ is more efficient because it returns a reference to a singleton instance of an empty collection, rather than creating a new instance each time it's called. This helps reduce the number of objects created, which can be beneficial in terms of both memory and performance.
Additionally, using Enumerable.Empty() can also improve code readability and maintainability by making it clear that the intention is to return an empty collection, rather than a collection with a specific number of elements.

👽
So, in conclusion, if you want to create an empty enumerable in C#, it's best to use⚡️ Enumerable.Empty()⚡️.

Resources

  1. https://bytelanguage.com/2018/07/27/enumerable-empty-vs-array-initialization-syntax/
  2. https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.empty?view=net-7.0 🔥🔥
  3. Source code of Enumerable class in Github
referencesource/Enumerable.cs at master · microsoft/referencesource
Source from the Microsoft .NET Reference Source that represent a subset of the .NET Framework - referencesource/Enumerable.cs at master · microsoft/referencesource

Source Code

GitHub - csandun/WaysToReturnEmptyEnumurable
Contribute to csandun/WaysToReturnEmptyEnumurable development by creating an account on GitHub.

📨
Put your comments on this topic. See ya soon on another blog post. 👊👊
Share this post