using block vs C# 8.0 using statement

Stesvis 1,041 Reputation points
2022-05-04T18:16:50.097+00:00

Hello,
I would like to ask some help understanding the using statement starting with C# 8.0
Reference: https://learn.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!

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,252 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,471 Reputation points Microsoft Vendor
    2022-05-05T06:22:05.767+00:00

    Hi @Stesvis ,
    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 additional answer

Sort by: Most helpful
  1. AgaveJoe 26,191 Reputation points
    2022-05-04T19:30:29.227+00:00

    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.

    0 comments No comments