Code Issues – Undisposed local
The Undisposed local code issue of the warning type highlights local variables that implement the System.IDisposable interface and are not explicitly disposed. The IDisposable interface was designed to provide a standard way to release unmanaged resources by calling its Dispose method. If the object is IDisposable, it is a good idea to dispose of it when you no longer need it, especially if the object uses unmanaged resources. These are resources that the .NET garbage collector does not manage on our behalf and is unable to clean-up automatically. They include items such as streams, files, database connections, handles and other operating system objects. If the memory and system resources that they use are not properly released, a program may suffer from memory leaks or problems due to locked resources.
The Stream (from System.IO) and Font (from System.Drawing) classes are examples of managed types that access unmanaged resources (in this case file handles and device contexts). If you hold a FileStream object, you should dispose this object when releasing resources, because this may not be done immediately by the garbage collector. Calling the Dispose method ensures that the unmanaged file handle controlled by the FileStream object is released as early as possible. The writer FileStream object is not explicitly disposed, so you can see the Undisposed local code issue:
Apply the Introduce Using Statement code provider, to fix it:
The code fix will create a using statement, which ensures that Dispose is called even if an exception occurs while you are calling methods on the object: