[Feature Request] Add "convert to using" refactoring

Assuming I have the following code:

var stream = new FileStream(@"C:\test.file", FileMode.Open);
var reader = new BinaryReader(stream);

var data = reader.ReadBytes(stream.Length);

// do something with data

stream.Close();
reader.Close();

I would find it very useful if there was a refactoring option to convert it to the following:

using (var stream = new FileStream(@"C:\test.file", FileMode.Open))
{
  using (var reader = new BinaryReader(stream))
  {
    var data = reader.ReadBytes(stream.Length);

    // do something with data
  }
}
3 comments
Avatar
Andrey Serebryansky
Comment actions Permalink

Hello Ian
     At the moment there's a context-action 'Put into using construct' for each declaration of an IDisposable variable. If it doesn't satisfy your needs, you're welcome to put a feature request through http://youtrack.jetbrains.net/issues/RSRP. Thank you!

Andrey Serebryansky

Senior Support Engineer

JetBrains, Inc

http://www.jetbrains.com

"Develop with pleasure!"

0
Comment actions Permalink

Hi Andrey,

I have not encountered that context action, for example if I have:

      var service = new WcfServiceClient();
      service.DoSomeOperation();
      service.Close();


where WcfServiceClient is a WCF service reference (which inherits from System.ServiceModel.ClientBase, which implements IDisposable) and I right-click or type Ctrl+Shift+R on the first line, there is no "Put into using construct" menuitem. Am I missing something?

Thanks,
Ian
0
Avatar
Andrey Serebryansky
Comment actions Permalink

Hello Ian
     You should place the caret at the variable name in declaration and then click the pencil icon as indicated in the attached screenshot. Thank you!

Andrey Serebryansky

Senior Support Engineer

JetBrains, Inc

http://www.jetbrains.com

"Develop with pleasure!"



Attachment(s):
PutIntoUsing.png
0

Please sign in to leave a comment.