This article shows benchmark results for adding items to a List collection by using AddRange() and for().
Category: .NET Coding Standards
Collection Performance: Efficiently Checking for Items in a Collection
Before processing collections, always perform null and item checks. For counting items, prefer Count for speed, as it's more efficient than LongCount() and TryGetNonEnumeratedCount(). When working with arrays, use Length or LongLength instead of Count. Benchmarking is crucial for optimizing collection methods in .NET applications.
Collection Performance: Creating New Sorted Collection From a Collection
This article talks about sorting collections with LINQ and doing the same using one of the sorted collections.
Collection Performance: Creating New Immutable Collection From a Collection
Immutable collections, which are collections that cannot be changed, was introduced in .NET 4.5 and are supported in .NET Core and beyond. There are immutable collections that mirror many of the generic collections in .NET. This article shows performance results from creating these types of collections.
Collection Performance: Creating new collection From a collection Using AddRange()
Its common in programming to create a collection from a collection. Usually there is some business logic applied to the items before they are put into the new collection. This article will focus on doing that by using AddRange() from LINQ.
Collection Performance: Creating A New List Or Linkedlist While Adding Items Using The Constructor
In many of the collection types in .NET, you can fill a collection at the same time the object is created as that can improve performance. This article shows the performance results for List and LinkedList.
Uncovering IDisposable Pitfalls in Microsoft .NET
The article by dotNetDave highlights the recurring issue of improper disposal of objects implementing IDisposable in .NET code, leading to memory leaks. The author provides examples and solutions, emphasizing the use of the 'using' code block for proper memory cleanup. The importance of recognizing disposable types and a call for Visual Studio to prompt for potential memory leaks are also discussed.
String Performance: The Fastest Way to Get a String’s Length
The article highlights the importance of using efficient methods for checking if a string is empty or null, emphasizing the performance benefits and exception prevention of using `string.IsNullOrEmpty()` and `string.IsNullOrWhiteSpace()` over the traditional `if (emailAddress == "")` approach. Benchmark results reveal that using null and `string.Empty()` is the most performant way to check for an empty string, with other methods showing similar performance levels. Updated January 2024.
Collection Performance: Sort() with CompareTo()
Let’s discuss for() and foreach() under load to establish a baseline. As you can see below, I used the same code in the previous chapter and added Task.Delay() to simulate a CPU load.
Collection Performance: Creating New Collection From A Collection Using for() & foreach()
It's common in programming to create a collection from a collection using for(), foreach() and ForEach(). Usually, business logic is applied to the items before they are put into the new collection. This article will focus on doing that along with discussing capacity. Updated February 2023.

You must be logged in to post a comment.