Topic | Value |
---|---|
Id | IDISP017 |
Severity | Warning |
Enabled | True |
Category | IDisposableAnalyzers.Correctness |
Code | DisposeCallAnalyzer |
Prefer using.
namespace N
{
using System.IO;
public sealed class Foo
{
public void Bar()
{
var stream = File.OpenRead(string.Empty)
var b = stream.ReadByte();
stream.Dispose();
}
}
}
Prefer using in the code above. It is cleaner than adding try-finally. The code above will not dispose the file if the read fails.
namespace N
{
using System.IO;
public sealed class Foo
{
public void Bar()
{
using (var stream = File.OpenRead(string.Empty))
{
var b = stream.ReadByte();
}
}
}
}
Configure the severity per project, for more info see MSDN.
#pragma warning disable IDISP017 // Prefer using
Code violating the rule here
#pragma warning restore IDISP017 // Prefer using
Or put this at the top of the file to disable all instances.
#pragma warning disable IDISP017 // Prefer using
[System.Diagnostics.CodeAnalysis.SuppressMessage("IDisposableAnalyzers.Correctness",
"IDISP017:Prefer using",
Justification = "Reason...")]