Skip to content

Task.CompletedTask vs Task.FromResult vs return: retarget to net10.0 and drop the redundant async - #2208

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/94960-csharp-completedtask-fromresult-return-in-async-methods
Open

vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/94960-csharp-completedtask-fromresult-return-in-async-methods

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Updates async-csharp/TaskCompletedTaskFromResultVsReturnInCSharp for the article rewrite.

  • Both projects retargeted from net7.0 to net10.0.
  • Test packages: Microsoft.NET.Test.Sdk 17.3.2 to 18.10.1, xunit 2.4.2 to 2.9.3, xunit.runner.visualstudio 2.4.5 to 4.0.0, coverlet.collector 3.1.2 to 10.0.1 (all re-queried on the NuGet flat-container index today; Test.Sdk 18.10.1 is newer than the 18.10.0 the plan named).
  • UseTaskFromResultAsync() no longer writes 'return await Task.FromResult(message)' inside an async method. It drops async and await and returns the finished task directly, which is half the allocation for the same result.
  • UseTaskCompletedSync() and UseTaskFromResultSync() are removed, together with their calls in Program.cs. Neither demonstrated blocking: both called Wait() or .Result on a task that was already complete.
  • Adds INotificationHandler and ConsoleNotificationHandler, the real case for Task.CompletedTask, where the signature is imposed by an interface and there is nothing to await.
  • Adds two tests asserting that the task handed back by a non-async method is already complete without being awaited. Three tests become five.

Build: 0 errors, 0 warnings. Tests: 5 of 5 passing on SDK 10.0.302 / runtime 10.0.10.

…op redundant async, add notification handler example

- Retarget both projects from net7.0 to net10.0
- Bump Microsoft.NET.Test.Sdk to 18.10.1, xunit to 2.9.3, xunit.runner.visualstudio to 4.0.0, coverlet.collector to 10.0.1
- UseTaskFromResultAsync() drops async/await and returns Task.FromResult(message) directly
- Remove UseTaskCompletedSync() and UseTaskFromResultSync(), and their calls in Program.cs
- Add INotificationHandler and ConsoleNotificationHandler, the imposed-signature case for Task.CompletedTask
- Add two tests asserting both factory-returned tasks are already complete without being awaited
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant