12

In C# when an Task or Task<T> method is returned from within a using statement is there any risk of the cleanup not properly occurring, or is this a poor practice? What concerns are there as it pertains to the closure of the variable in the using block?

Consider the following:

public Task<string> GetAsync(string url)
{
    using (var client = new HttpClient())
    {
        return client.GetStringAsync(url);
    }
}

In the example above the asynchronous operation is represented by the client.GetStringAsync(url), I'm simply returning that Task<string> for the consumer to await. What happens to the client as it is in a using - does it get cleaned up before it is awaited or garbage collected, or does it cause other issues?

Would it be better to use async and await in the cause of using statements like this, if so why?

public async Task<string> GetAsync(string url)
{
    string response = string.Empty;
    using (var client = new HttpClient())
    {
        response = await client.GetStringAsync(url);
    }
    return response;
}

Or

public async Task<string> GetAsync(string url)
{
    using (var client = new HttpClient())
    {
        return await client.GetStringAsync(url);
    }
}

What is the difference?

David Pine
  • 23,787
  • 10
  • 79
  • 107

1 Answers1

16

Using the first method will not work, client will be disposed of before it completes its work, you must use the 2nd or 3rd version.

There is no practical difference between the 2nd and 3rd versions, use whichever fits your team's style. However in the 2nd version string response = string.Empty; can be simplified to string response; There is no reason to assign the variable a value if all code paths will overwrite it without reading it.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • 2
    "Will not work" is a bit misleading.... It definitely is not *correct*, but it will technically "work" if the result of the task happens to be made available synchronously, which makes this type of error hard to test for. – JoelFan Jun 26 '19 at 20:49