Code inspection: Convert into 'using' declaration
If a using
statement is at the end of a code block, this inspection suggests converting it into a more concise syntax of the using
declaration.
The resource will be disposed at the end of the containing block anyway, so this is an opportunity to reduce code nesting without decreasing its readability.
void ReadFile(string path)
{
using (StreamReader reader = File.OpenText(path))
{
while (reader.ReadLine() is { })
{
// do something
}
}
}
void ReadFile(string path)
{
using StreamReader reader = File.OpenText(path);
while (reader.ReadLine() is { })
{
// do something
}
}
Last modified: 25 September 2024