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.
Suboptimal code
voidReadFile(string path)
{
using(StreamReader reader = File.OpenText(path))
{
while(reader.ReadLine()is{})
{
// do something
}
}
}
After the quick-fix
voidReadFile(string path)
{
usingStreamReader reader = File.OpenText(path);
while(reader.ReadLine()is{})
{
// do something
}
}
tip
You can press AltEnter on a using declaration to convert it back to a using statement.
Our website uses some cookies and records your IP address for the purposes of accessibility, security, and managing your access to the telecommunication network. You can disable data collection and cookies by changing your browser settings, but it may affect how this website functions. Learn more.
With your consent, JetBrains may also use cookies and your IP address to collect individual statistics and provide you with personalized offers and ads subject to the Privacy Notice and the Terms of Use. JetBrains may use third-party services for this purpose. You can adjust or withdraw your consent at any time by visiting the Opt-Out page.