One nice little enhancement introduced in C## 8 helps to simplify code that uses disposable objects.

For example consider the following:

Prior to C## 8, if you wanted to use a disposable object (something that implements IDisposable) then you would usually use a using block as follows:

At the end of the using block, the Dispose() method is called automatically.

With C## 8, instead of the using block, you can instead use a using declaration:

Notice in the preceding code, with a using declaration there is no need for the additional {}. When using a using declaration, the Dispose() method is called automatically at the end of the Process() method. Just as with the using block approach, if an exception occurs within the Process() method then Dispose() will still be called.

Using declarations help to keep code less cluttered because you have fewer braces {} and one level less of indenting.

If you have multiple usings, for example:

You can rewrite this in C## 8 as follows:

#icymi c# 8 #c# 8

 ICYMI C# 8 New Features: Write Less Code with Using Declarations
3.05 GEEK