Skip to content

Clarify expected behavior in LINQ async example #47626

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 4 commits into from
Aug 6, 2025
Merged

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 29, 2025

The LINQ async example in the C# asynchronous programming documentation was causing confusion about whether the shown approach was correct or problematic. The example demonstrated the correct implementation using .ToArray() for immediate evaluation, but the accompanying warning text suggested there was an issue with the approach, leaving readers uncertain about best practices.

Changes Made

Before: The documentation showed a correct LINQ example but included confusing warning text that made it unclear whether this was the recommended approach.

After: The documentation now clearly:

  • States that the shown LINQ example is correct and safe because it uses .ToArray()
  • Explains why .ToArray() ensures immediate task execution and concurrent processing
  • Provides a contrasting "problematic approach" example without .ToArray()
  • Shows the "recommended approach" with clear DO/DON'T guidance
  • Includes explicit guidance to always use .ToArray() or .ToList() with async LINQ

Example of the clarification:

// DON'T do this - tasks won't start until enumerated
var getUserTasks = userIds.Select(id => GetUserAsync(id)); // No .ToArray()!
return await Task.WhenAll(getUserTasks); // Tasks start here, not above

// DO this - tasks start immediately
var getUserTasks = userIds.Select(id => GetUserAsync(id)).ToArray();
return await Task.WhenAll(getUserTasks);

This change eliminates the confusion identified in the issue by making it crystal clear that the LINQ approach with .ToArray() is the recommended way to create concurrent async tasks.

Fixes #27720.

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:


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.


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 Jul 29, 2025
@Copilot Copilot AI changed the title [WIP] Clarify expected behavior in LINQ async example Clarify expected behavior in LINQ async example Jul 29, 2025
@Copilot Copilot AI requested a review from BillWagner July 29, 2025 21:11
Copilot finished work on behalf of BillWagner July 29, 2025 21:11
@BillWagner
Copy link
Member

@copilot Please fix the three markdown lint issues.

Copy link
Contributor Author

Copilot AI commented Jul 30, 2025

@copilot Please fix the three markdown lint issues.

Fixed the three markdown lint issues in commit 7cd9d78:

  1. Removed trailing space from line 133
  2. Added blank lines before fenced code blocks on lines 138 and 145

The linter now passes with 0 errors.

Copilot finished work on behalf of BillWagner July 30, 2025 18:35
@BillWagner BillWagner marked this pull request as ready for review July 30, 2025 18:40
@BillWagner BillWagner requested a review from a team as a code owner July 30, 2025 18:40
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 LGTM and is ready for final review.

@BillWagner BillWagner requested review from tdykstra and a team July 30, 2025 18:41
@Copilot Copilot AI requested a review from gewarren August 5, 2025 19:54
Copilot finished work on behalf of gewarren August 5, 2025 19:54
@BillWagner BillWagner enabled auto-merge (squash) August 6, 2025 13:57
@BillWagner BillWagner merged commit bf6134c into main Aug 6, 2025
10 checks passed
@BillWagner BillWagner deleted the copilot/fix-27720 branch August 6, 2025 14:00
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.

Clarify expected behavior in LINQ async example
3 participants