UnnecessaryUsing

UnnecessaryUsing

Summary

Unnecessary using statements must be removed.

Default severity

Warning

Description

StringReader, StringWriter, MemoryStream, UnmanagedMemoryStream, and UnmanagedMemoryAccessor implement IDisposable but dispose of nothing. See the note of them [1], which is quoted as follows:

Note

This type implements the IDisposable interface, but does not actually have any resources to dispose. This means that disposing it by directly calling Dispose() or by using a language construct such as using (in C#) or Using (in Visual Basic) is not necessary.

So, creating an instance of these classes with using statements doesn't make sense. They must be created without using statements.

Code fix

The code fix provides an option eliminating using statements.

Example

Diagnostic

using System;

public class Main
{
    public void Method()
    {
        using (var s = new MemoryStream())
        {
            ...
        }
    }
}

Code fix

using System;

public class Main
{
    public void Method()
    {
        {
            var s = new MemoryStream();
            {
                ...
            }
        }
    }
}

References

[1] Microsoft, .NET API Browser