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

Merged
merged 6 commits into from
Aug 15, 2025

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
Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
@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
Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
Copy link
Contributor

PoliCheck Scan Report

The following reports lists PoliCheck issues in PR files. Before you merge the PR, you must fix all severity-1 issues. Other issues are also a high priority. The AI Review Details column lists suggestions for either removing or replacing the terms. If you find a false positive result, mention it in a comment that you post to the PR. The feedback helps reduce false positive instances in future scans.

✅ No issues found

More information about PoliCheck

Information: PoliCheck | Severity Guidance | Term
For any questions: Try searching the learn.microsoft.com contributor guides or post your question in the Learn support channel.

@BillWagner
Copy link
Member

@gewarren This is now updated with all your feedback. Can you approve?

@BillWagner BillWagner enabled auto-merge (squash) August 14, 2025 12:47
@BillWagner BillWagner requested a review from a team August 14, 2025 18:30
Copy link
Contributor

@adegeo adegeo left a comment

Choose a reason for hiding this comment

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

@copilot the full Program.cs reference at the end of the article includes the snippet system code comments because it's referencing the file with out a snippet id. Place a starting and ending snippet id (named complete) at the top and bottom of the Program.cs file, then reference it. The build system will solve the problem of showing other snippet identifiers in the published article.

Co-authored-by: adegeo <67293991+adegeo@users.noreply.github.com>
auto-merge was automatically disabled August 14, 2025 20:44

Head branch was pushed to by a user without write access

Copy link
Contributor Author

Copilot AI commented Aug 14, 2025

@copilot the full Program.cs reference at the end of the article includes the snippet system code comments because it's referencing the file with out a snippet id. Place a starting and ending snippet ...

Fixed this issue by adding "complete" snippet markers to wrap the entire Program.cs file and updating the markdown reference to use ID="complete". This prevents the internal snippet system code comments from appearing in the published article. The change was validated by successfully building the project. (commit e26e9a4)

@Copilot Copilot AI requested a review from adegeo August 14, 2025 20:45
Copilot finished work on behalf of adegeo August 14, 2025 20:45
Copy link
Contributor

PoliCheck Scan Report

The following report lists PoliCheck issues in PR files. Before you merge the PR, you must fix all severity-1 issues. Other issues are also a high priority. The AI Review Details column lists suggestions for either removing or replacing the terms. If you find a false positive result, mention it in a PR comment and include this text: #policheck-false-positive. This feedback helps reduce false positives in future scans.

✅ No issues found

More information about PoliCheck

Information: PoliCheck | Severity Guidance | Term
For any questions: Try searching the learn.microsoft.com contributor guides or post your question in the Learn support channel.

@BillWagner BillWagner merged commit e7ed445 into main Aug 15, 2025
10 checks passed
@BillWagner BillWagner deleted the copilot/fix-34606 branch August 15, 2025 14:11
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
4 participants