Dispose Fields

Disposes of IDisposable fields contained in the current IDisposable class.

Availability

From the context menus or via shortcuts:

  • When the edit caret is on a declaration of a class implementing the IDisposable interface, provided that it contains the Dispose method and undisposed fields of types that implement IDisposable.

Example

class <:caret:>FormDrawer:IDisposable
{
  private Graphics _CurrentGraphics;
  private Pen _DefaultPen;
  public FormDrawer(Form operatedForm)
  {
    _CurrentGraphics = operatedForm.CreateGraphics();
    _DefaultPen = new Pen(Color.Black);
  }
  public void DrawCircle(int centerX, int centerY, int radius)
  {
    _CurrentGraphics.DrawEllipse(_DefaultPen, centerX - radius, centerY - radius, radius * 2, radius * 2);
  }
  public void DrawCircle(int centerX, int centerY, int radius, Pen pen)
  {
    _CurrentGraphics.DrawEllipse(pen, centerX - radius, centerY - radius, radius * 2, radius * 2);
  }
  public void Dispose()
  {
    Dispose(true);
    GC.SuppressFinalize(this);
  }
  protected virtual void Dispose(bool disposing)
  {
    if (disposing)
    {

    }
  }
}
Class <:caret:>FormDrawer
  Implements IDisposable
  Private _CurrentGraphics As Graphics
  Private _DefaultPen As Pen
  Public Sub New(ByVal operatedForm As Form)
    _CurrentGraphics = operatedForm.CreateGraphics()
    _DefaultPen = New Pen(Color.Black)
  End Sub
  Public Sub DrawCircle(ByVal centerX As Integer, ByVal centerY As Integer, ByVal radius As Integer)
    _CurrentGraphics.DrawEllipse(_DefaultPen, centerX - radius, centerY - radius, radius * 2, radius * 2)
  End Sub
  Public Sub DrawCircle(ByVal centerX As Integer, ByVal centerY As Integer, ByVal radius As Integer, ByVal pen As Pen)
    _CurrentGraphics.DrawEllipse(pen, centerX - radius, centerY - radius, radius * 2, radius * 2)
  End Sub
  Public Sub Dispose() Implements IDisposable.Dispose
    Dispose(True)
    GC.SuppressFinalize(Me)
  End Sub
  Protected Overridable Sub Dispose(ByVal disposing As Boolean)
    If disposing Then

    End If
  End Sub
End Class

Result:

class FormDrawer:IDisposable
{
  private Graphics _CurrentGraphics;
  private Pen _DefaultPen;
  public FormDrawer(Form operatedForm)
  {
    _CurrentGraphics = operatedForm.CreateGraphics();
    _DefaultPen = new Pen(Color.Black);
  }
  public void DrawCircle(int centerX, int centerY, int radius)
  {
    _CurrentGraphics.DrawEllipse(_DefaultPen, centerX - radius, centerY - radius, radius * 2, radius * 2);
  }
  public void DrawCircle(int centerX, int centerY, int radius, Pen pen)
  {
    _CurrentGraphics.DrawEllipse(pen, centerX - radius, centerY - radius, radius * 2, radius * 2);
  }
  public void Dispose()
  {
    Dispose(true);
    GC.SuppressFinalize(this);
  }
  protected virtual void Dispose(bool disposing)
  {
    if (disposing)
    {
      if (_CurrentGraphics != null)
      {
        _CurrentGraphics.Dispose();
        _CurrentGraphics = null;
      }
      if (_DefaultPen != null)
      {
        _DefaultPen.Dispose();
        _DefaultPen = null;
      }
    }
  }
}
Class FormDrawer
  Implements IDisposable
  Private _CurrentGraphics As Graphics
  Private _DefaultPen As Pen
  Public Sub New(ByVal operatedForm As Form)
    _CurrentGraphics = operatedForm.CreateGraphics()
    _DefaultPen = New Pen(Color.Black)
  End Sub
  Public Sub DrawCircle(ByVal centerX As Integer, ByVal centerY As Integer, ByVal radius As Integer)
    _CurrentGraphics.DrawEllipse(_DefaultPen, centerX - radius, centerY - radius, radius * 2, radius * 2)
  End Sub
  Public Sub DrawCircle(ByVal centerX As Integer, ByVal centerY As Integer, ByVal radius As Integer, ByVal pen As Pen)
    _CurrentGraphics.DrawEllipse(pen, centerX - radius, centerY - radius, radius * 2, radius * 2)
  End Sub
  Public Sub Dispose() Implements IDisposable.Dispose
    Dispose(True)
    GC.SuppressFinalize(Me)
  End Sub
  Protected Overridable Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If _CurrentGraphics IsNot Nothing Then
        _CurrentGraphics.Dispose()
        _CurrentGraphics = Nothing
      End If
      If _DefaultPen IsNot Nothing Then
        _DefaultPen.Dispose()
        _DefaultPen = Nothing
      End If
    End If

  End Sub
End Class