I may be missing something basic but how come does this method:
namespace System.Net
{
public static class WebClientExtensions
{
public static Task<byte[]> DownloadDataTask(this WebClient webClient, string address)
{
return DownloadDataTask(webClient, new Uri(address));
}
}
}
is called like this:
return new WebClient().DownloadDataTask(url)
This was taken from the ParallelExtensionsExtras Tour – #16 – Async Tasks for WebClient.
Such method is called an extension method. It is distinguished by having
thisin front of the first parameter and you can call it as if it was an instance method on that parameter.So, the code you wrote:
is actually exactly the same as:
The advantage is that it is a nicer syntax. For example, pretty much all of LINQ is also based on extension methods.