Skip to content

Add guidance for choosing ToList() vs ToArray() in async LINQ scenarios #47729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 4, 2025

Enhanced the async scenarios documentation to explain when to use ToList() versus ToArray() when forcing eager evaluation of LINQ expressions with async operations.

Changes Made

Updated documentation text to provide clear guidance on choosing between the two methods:

  • Use ToArray() for Task.WhenAll scenarios where you process all tasks together and the collection size is fixed
  • Use ToList() for Task.WhenAny scenarios where you need to dynamically manage tasks, such as removing completed tasks from the collection

Added practical code example demonstrating the ToList() approach:

private static async Task ProcessTasksAsTheyCompleteAsync(IEnumerable<int> userIds)
{
    var getUserTasks = userIds.Select(id => GetUserAsync(id)).ToList();
    
    while (getUserTasks.Count > 0)
    {
        Task<User> completedTask = await Task.WhenAny(getUserTasks);
        getUserTasks.Remove(completedTask);
        
        User user = await completedTask;
        Console.WriteLine($"Processed user {user.id}");
    }
}

This example shows how ToList() creates a modifiable collection that supports the Remove() operation, making it ideal for WhenAny patterns where you handle completions as they arise.

The existing ToArray() example and explanation remain unchanged to maintain backwards compatibility while providing developers with a complete understanding of both approaches.

Fixes #34606.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • learn.microsoft.com
    • Triggering command: /home/REDACTED/work/docs/docs/docs/csharp/asynchronous-programming/snippets/async-scenarios/bin/Debug/net8.0/async-scenarios (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


Internal previews

📄 File 🔗 Preview link
docs/csharp/asynchronous-programming/async-scenarios.md docs/csharp/asynchronous-programming/async-scenarios

@dotnet-policy-service dotnet-policy-service bot added the okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings. label Aug 4, 2025
@Copilot Copilot AI changed the title [WIP] Give ToList preference for WhenAny Add guidance for choosing ToList() vs ToArray() in async LINQ scenarios Aug 4, 2025
@Copilot Copilot AI requested a review from BillWagner August 4, 2025 20:13
Copilot finished work on behalf of BillWagner August 4, 2025 20:13
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ready for final review.

@BillWagner BillWagner marked this pull request as ready for review August 5, 2025 14:56
@BillWagner BillWagner requested a review from a team as a code owner August 5, 2025 14:56
@BillWagner BillWagner requested review from tdykstra and adegeo August 5, 2025 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
async-task-programming/subsvc dotnet-csharp/svc okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Give ToList preference for WhenAny
2 participants