Incorrect loop bound in SizeOptIListSelectIterator.ToList

Open Beginner friendly
#137,126 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@equmagg is already working on this.

Since Nov 7, 2026.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
64/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp
Domain
backend

Research direction

Find SizeOptIListSelectIterator<TSource, TResult>.ToList() in the .NET runtime source and read the iterator implementation introduced in PR #118156. Reproduce the issue with <UseSizeOptimizedLinq>true</UseSizeOptimizedLinq> using the supplied reflection example. Done when the override returns the selected items and the result matches normal query enumeration; the issue suggests fixing or removing this non-public path.

Written by the indexing model from the issue text.

Description

area-System.Collections untriaged
Description

SizeOptIListSelectIterator<TSource, TResult>.ToList() checks the destination list’s Count immediately after constructing it with a capacity. Since Count is zero, the loop never executes and the override returns an empty list for a nonempty source.

Admittedly, this path is non-public and functionally dead, but it hides a ticking time bomb, so would be nice to fix for good hygiene (or remove altogether)

Reproduction Steps

With true

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

IEnumerable<int> query = new[] { 1, 2, 3 }.Select(x => x * 10);

MethodInfo method = query.GetType().GetMethod(
    "ToList",
    BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
    ?? throw new InvalidOperationException("ToList override not found");

List<int> result = (List<int>)(method.Invoke(query, null)
    ?? throw new InvalidOperationException("ToList returned null"));

Console.WriteLine(query.GetType().Name);
Console.WriteLine($"Reflection: [{string.Join(", ", result)}]");
Console.WriteLine($"Public API: [{string.Join(", ", query.ToList())}]");
Expected behavior

printing [10, 20, 30], matching enumeration of the query

Actual behavior

prints []

Regression?

The erroneous loop was included when this iterator was introduced in #118156

Known Workarounds

No workaround is needed

Configuration

Windows 10, x64
Reproduced on .NET 10, 11 rc3 and main

Other information
List<TResult> list = new List<TResult>(_source.Count);
for (int i = 0; i < list.Count; i++)
{
    list.Add(_selector(_source[i]));
}
return list;

The constructor initializes Capacity, not Count

Dominant language
C#
Stars
18.4k
Forks
5.8k
Avg merge
2d 12h
Merged PRs (30d)
579

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.