https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement
File and Font are examples of managed types that access unmanaged resources (in this case file handles and device contexts). There are many other kinds of unmanaged resources and class library types that encapsulate them. All such types must implement the IDisposable interface.
As a rule, when you use an IDisposable
object, you should declare and instantiate it in a using
statement. The using
statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using
block, the object is read-only and cannot be modified or reassigned.
The using
statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using
statement is translated by the compiler.