DEV Community

Cover image for C#8 changes to using
Nick Raphael
Nick Raphael

Posted on • Updated on

C#8 changes to using

The 'using' block has been a mainstay in c# since the beginning.

C-SharpCorner has a nice desciption:
To allow the programmer to explicitly perform these clean-up activities, objects can provide a Dispose method that can be invoked when the object is no longer needed. The using statement in C# defines a boundary for the object outside of which, the object is automatically destroyed. The using statement is exited when the end of the "using" statement block or the execution exits the "using" statement block indirectly, for example - an exception is thrown.

Here is an example...

using System;

namespace ConsoleApp2
{
    class DisposableObject : IDisposable
    {
        public void Dispose()
        {
            Console.Write("disposing ");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            TextUsing();
        }

        static void TextUsing()
        {
            using (var myObj = new DisposableObject())
            {
                for (var i = 0; i < 10; i++)
                {
                    Console.Write($"{i.ToString()} ");
                }
            }
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Output: 0 1 2 3 4 5 6 7 8 9 disposing

The Dispose method on our myObj object gets called when the using block completes. Lets be explicit here - it's when execution reaches the closing curly braces of the using block.

The using block has been this way forever. But things are changing in C#8.0. Your existing code will behave exactly as it always has. But C#8.0 doesn't require curly braces at all.

Example time...

static void TextUsing()
{
   using var myObj = new DisposableObject();

   for (var i = 0; i<10; i++)
   {
      Console.Write($"{i.ToString()} ");
   }
} 
Enter fullscreen mode Exit fullscreen mode

Output: 0 1 2 3 4 5 6 7 8 9 disposing

As you can see, we can now declare an object using 'using' and there is no need for curlies. The Dispose method will be called when myObj's scope completes. Which in this case is at the end of the method. I like this change. I always felt the curlies made it look like our code had multiple paths. As I mentioned, you can still use the curlies if you want to explicitly dispose of your object. But the vast majority of the time it can wait for the end of the current scope.

One final example...

static void TextUsing()
{
   for (var i = 0; i<10; i++)
   {
      Console.Write($"{i.ToString()} ");
      using var myObj = new DisposableObject();
   }
}
Enter fullscreen mode Exit fullscreen mode

Output: 0 disposing 1 disposing 2 disposing 3 disposing 4 disposing 5 disposing 6 disposing 7 disposing 8 disposing 9 disposing

The scope of the using state here is the for loop. Therefore Dispose is called on every loop. To achieve this in C#7.0 would require an extra level of curly brace depth.

It's not an earth shattering addition to the language, but I like it.

Top comments (0)