...

/

Simplified Disposal and Compressed Streams

Simplified Disposal and Compressed Streams

Learn about the “using” statement for automatic resource management and how to compress XML data using GZip and Brotli algorithms.

We can simplify the code that needs to check for a null object and then call its Dispose method by using the using statement. Generally, we would recommend using  using rather than manually calling Dispose is recommended unless we need greater control.

The using keyword

Confusingly, there are two uses for the using keyword:

  • Importing a namespace.

  • Generating a finally statement that calls Dispose on an object that implements IDisposable.

The compiler ...