Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Task returning method in using block

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?

like image 596
David Pine Avatar asked Jun 26 '16 17:06

David Pine


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

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.

like image 165
Scott Chamberlain Avatar answered Sep 25 '22 15:09

Scott Chamberlain