What is the difference of DownloadFileAsync and DownloadFileTaskAsync?
When I should use one instead of another? Any example would be appreciated.
General pattern - if you find two methods with names ending xxxAsync
and xxxTaskAsync
, then you should generally prefer the Task
version.
The two versions will exist because the xxxAsync
version was created before the Task-based Async Pattern (TAP) was introduced, and will be based on an older async pattern.
When introducing TAP methods, the usual recommendation is to suffix the name with Async
- but that cannot be done when there's already another method that has that same name1 - so the recommendation then is to suffix with TaskAsync
.
In this specific case DownloadFileAsync
is an implementation of the "Event-based Async Pattern" (EAP), which tends to be more awkward to work with. That pattern, itself, superceded the original async pattern within the .NET Framework, which was based on matching Begin
and End
prefixed methods and IAsyncResult
, the Async Programming Model
1In general, of course, you can introduce multiple methods with the same name, provided they have different signatures. But when it comes to the async patterns, an EAP async method and a TAP async method will generally take the same arguments - they'll only differ on their return type. Even if they did vary in their arguments, putting both patterns under exactly the same name would probably cause more confusion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With