question

Stesvis-5434 avatar image
0 Votes"
Stesvis-5434 asked Stesvis-5434 commented

using block vs C# 8.0 using statement

Hello,
I would like to ask some help understanding the using statement starting with C# 8.0
Reference: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement

For example from this:

public void MyFunction()
{
string manyLines = @"This is line one
This is line two
Here is line three
The penultimate line is line four
This is the final, fifth line.";

// -----------> 1
using (var reader = new StringReader(manyLines))
{
string? item;
do
{
item = reader.ReadLine();
Console.WriteLine(item);
} while (item != null);
}
// -----------> 2

// do some other long task...

// -----------> 3
}


to this:

public void MyFunction()
{
string manyLines = @"This is line one
This is line two
Here is line three
The penultimate line is line four
This is the final, fifth line.";

// -----------> 1
using var reader = new StringReader(manyLines);
string? item;
do
{
item = reader.ReadLine();
Console.WriteLine(item);
} while (item != null);
// -----------> 2

// do some other long task...

// -----------> 3
}


  1. In the first scenario, the reader is disposed at the end of the using block (-----------> 2), which means that after the closing bracket I can do more stuff but the resource has already been disposed.

  2. In the second scenario, I don't know what's happening. Is reader being disposed at the end of the function (-----------> 3)? Or is it disposed right after the last time that reader is being used (-----------> 2)?

I read all the documentation but I am sorry I still don't understand for sure.

In my code, I would like to dispose my resource as soon as possible and I would like to know if switching to the new using statement will keep the behavior the same.
Thanks!





dotnet-csharpdotnet-aspnet-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered Stesvis-5434 commented

Hi @Stesvis-5434,
In the case of a using statement, we have control over defining the scope of the object. In the case of a using declaration(C# 8.0), its scope is automatically defined from the object’s declaration statement to the end of the current code block.
If the methods are going to be small and you are using the using declaration in the final part of the method, the new approach is well and good.

You can check this post to help you understand better.

Best regards,
Lan Huang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks @LanHuang-MSFT that's what i needed.
Sometimes I need my disposable object only at the end of the function so I just declare it at the bottom and that's it. Now it's more clear thanks.

0 Votes 0 ·
AgaveJoe avatar image
0 Votes"
AgaveJoe answered Stesvis-5434 commented

As I understand the documentation, the using declaration is scoped to current code block like a method or if condition. The "using object" is disposed when the object goes out of scope. In a using statement the object scope is within the brackets.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.