Skip to content

Conversation

BartoszKlonowski
Copy link
Contributor

This pull request fixes #47630
It updates the "Async programming scenarios" page as listed in the issue.

@BartoszKlonowski BartoszKlonowski requested review from a team and BillWagner as code owners October 11, 2025 16:09
@dotnetrepoman dotnetrepoman bot added this to the October 2025 milestone Oct 11, 2025
@dotnet-policy-service dotnet-policy-service bot added dotnet-csharp/svc async-task-programming/subsvc community-contribution Indicates PR is created by someone from the .NET community. labels Oct 11, 2025
Comment on lines 171 to 175
var html = await s_httpClient.GetStringAsync(URL);
return Regex.Matches(html, @"\.NET").Count;
return await Task.Run(() =>
{
var html = s_httpClient.GetStringAsync(URL).Result;
return Regex.Matches(html, @"\.NET").Count;
});
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand the reason for this change. The original code already used an async method that was correctly awaited. Now, it looks like you are using a blocking call is a second thread. I don't see the benefit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Honestly, I got confused too, by the words: "method should return await". Method can't have the return type of await, hence I came up with this proposal.
I'm totally OK with reverting it, maybe replacing with some other implementation if you, @BillWagner have an idea of what the requirement can be for this point I mentioned.

Copy link
Member

Choose a reason for hiding this comment

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

@BartoszKlonowski

I see. I'm answering assuming you mean the text in the "Use await inside async() method body".

First a nit: Let's remove the () on async. That reads wrong.

The reason you should have an await expression if you add the async modifier to a method is efficiency.

Adding the async modifier requires the compiler to generate the async state machine, which is complicated IL. If you don't need it, it's a fair amount of overhead.

Instead, maybe we should state this as "Only add the async modifier if the method includes an await expression.

It's perfectly valid to have a method that returns a Task or Task<T> that is synchronous! (Some of the examples in this article could be modified to use that idiom: mainly if the last line of the method is return await <method>.

There is an important difference in the two idioms: A synchronous method that returns a task type is a normal "synchronous" method. If it has the async modifier, the return statement must return the T (or nothing) and the compiler generates the code that creates the continuation when the asynchronous work completes, and packages the return in a Task (or ValueTask) as needed. It also generates the necessary code to create a Faulted task with the Exception property in case of failure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

async-task-programming/subsvc community-contribution Indicates PR is created by someone from the .NET community. dotnet-csharp/svc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Async programming nits

2 participants