Back to Devexpress

Introduce Using Statement

coderushforroslyn-118160-refactoring-assistance-introduce-using-statement.md

latest1.3 KB
Original Source

Introduce Using Statement

  • Aug 19, 2021

Purpose

Replaces a disposable object creation statement with the using statement. If the Dispose() method was called explicitly, this refactoring removes it and closes the using body on the line that was holding the Dispose() method. This increases the code readability and ensures that the objects you are using locally are properly disposed of.

Availability

Available when the caret is on an object creation statement assuming that the object implements the IDisposable interface.

Usage

  1. Place the caret in the IDisposable object creation statement.

  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.

  3. Select Introduce Using Statement from the menu.

After execution, the Refactoring replaces the object creation and disposition statements with the using statement.

csharp
using (Graphics formGraphics = CreateGraphics()) {
    formGraphics.FillRectangle(new SolidBrush(Color.Purple), new Rectangle(50, 50, 100, 100));
}
vb
Using formGraphics As Graphics = CreateGraphics()
    formGraphics.FillRectangle(New SolidBrush(Color.Purple), New Rectangle(50, 50, 100, 100))
End Using