Implement IDisposable
- 3 minutes to read
In This Article
Implements the IDisposable interface in the current class.
#Availability
From the context menus or via shortcuts:
- when the edit cursor or caret is on a declaration of a class containing IDisposable fields, provided that the class does not implement IDisposable.
#Example
class │FormDrawer
{
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);
}
}
Class │FormDrawer
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
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()
{
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(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