ToListinator 0.0.10

dotnet add package ToListinator --version 0.0.10
                    
NuGet\Install-Package ToListinator -Version 0.0.10
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ToListinator" Version="0.0.10" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ToListinator" Version="0.0.10" />
                    
Directory.Packages.props
<PackageReference Include="ToListinator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ToListinator --version 0.0.10
                    
#r "nuget: ToListinator, 0.0.10"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ToListinator@0.0.10
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ToListinator&version=0.0.10
                    
Install as a Cake Addin
#tool nuget:?package=ToListinator&version=0.0.10
                    
Install as a Cake Tool

ToListinator

The tragedy of ToList

As the blog post discusses, it is very common for C# programmers to waste resources by allocating lists unnecessarily. The canonical example is using ToList().ForEeach(...) solely because the List<T> type happens to have a ForEach method on it.

ToListinator is a Roslyn code analyzer designed to track down and help eliminate these kinds of unnecessary ToList calls with extreme prejudice, among other things.

Available Analyzers

TL001 - ToList().ForEach Detection

Category: Performance | Severity: Warning

Detects the pattern collection.ToList().ForEach(action) which unnecessarily allocates a List just to call the ForEach method. This pattern should be replaced with a regular foreach loop.

Example:

// ❌ Bad - unnecessary List allocation
items.Where(x => x.IsValid).ToList().ForEach(ProcessItem);

// ✅ Good - use foreach loop instead
foreach (var item in items.Where(x => x.IsValid))
{
    ProcessItem(item);
}

TL002 - Identity Select Detection

Category: Performance | Severity: Warning

Detects useless Select(x => x) identity operations that perform no transformation and should be removed.

Example:

// ❌ Bad - pointless identity transformation
var result = items.Where(x => x.IsValid).Select(x => x).ToList();

// ✅ Good - remove unnecessary Select
var result = items.Where(x => x.IsValid).ToList();

TL003 - ToList().Count Comparison Detection

Category: Performance | Severity: Warning

Detects ToList().Count comparisons used for existence checks and suggests using Any() instead to avoid unnecessary allocation.

Example:

// ❌ Bad - unnecessary List allocation for existence check
if (items.Where(x => x.IsValid).ToList().Count > 0)

// ✅ Good - use Any() for existence checks
if (items.Where(x => x.IsValid).Any())

TL004 - Null Coalescing Foreach Detection

Category: Performance | Severity: Warning

Detects the pattern foreach (var item in collection ?? new Collection()) which creates unnecessary allocations. Suggests using null checks instead.

Example:

// ❌ Bad - creates empty collection on every iteration when null
foreach (var item in items ?? new List<string>())

// ✅ Good - check for null to avoid allocation
if (items != null)
{
    foreach (var item in items)
    {
        // process item
    }
}

TL005 - Static Property Expression Body Detection

Category: Performance | Severity: Warning

Detects static properties with expression bodies that may allocate new instances on every access. Suggests using getter-only properties with initializers instead.

Example:

// ❌ Bad - allocates new array on every access
public static string[] Parts => "a,b,c".Split(',');

// ✅ Good - allocated once and cached
public static string[] Parts { get; } = "a,b,c".Split(',');

TL006 - Where().Count() Detection

Category: Performance | Severity: Warning

Detects Where(predicate).Count() patterns and suggests using Count(predicate) for better performance.

Example:

// ❌ Bad - two-pass operation
var count = items.Where(x => x.IsValid).Count();

// ✅ Good - single-pass operation
var count = items.Count(x => x.IsValid);

TL007 - Unnecessary ToList/ToArray in Method Chains

Category: Performance | Severity: Warning

Detects unnecessary ToList() or ToArray() calls in the middle of method chains that create intermediate collections only to be immediately enumerated again.

Example:

// ❌ Bad - unnecessary intermediate collection
var result = items.Where(x => x.IsValid).ToList().Select(x => x.Name).ToArray();

// ✅ Good - direct enumeration without intermediate collection
var result = items.Where(x => x.IsValid).Select(x => x.Name).ToArray();

TL010 - Unnecessary ToList on Materialized Collections

Category: Performance | Severity: Warning

Detects ToList() calls on collections that are already materialized (like List<T>, arrays, etc.) which creates unnecessary copies.

Example:

List<string> names = GetNames();

// ❌ Bad - creates unnecessary copy of already materialized collection
var filtered = names.ToList().Where(x => x.Length > 3).ToList();

// ✅ Good - use the materialized collection directly
var filtered = names.Where(x => x.Length > 3).ToList();
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.10 9 8/26/2025
0.0.9 12 8/26/2025
0.0.8 28 8/26/2025
0.0.7 28 8/26/2025
0.0.6 31 8/22/2025
0.0.5 200 8/4/2025
0.0.4 444 7/23/2025
0.0.3 494 7/23/2025
0.0.2 279 7/21/2025