T
Tony Johansson
Hi!
GC removes objects when the GC decides so because C# is managed.
At the bottom I have a code snipped.
The book I read say Always call Dispose() on Pen objects or use the using
feature on the Pen object.
I mean C# is managed so I can't understand why I should dispose the Pen
object. GC should take care of this.
The reason is probably that I mixup memory and resources. GC take care of
free memory but not resources.
I have some problem to understand why I must dispose the Pen object here
because I see the Pen object as
any object such as a Person or Car.
So my first question is can I use the rule that say always dispose an object
where the class implements the IDisposable which actually mean that I have
to check if the class implements the IDisposable ?
My second question is if I don't dispose the Pen object will a lot of
resources be allocated that will affect the application and what kind of
resources is that ?
I don't think that the amount of resources is that limited. I mean that it
would have been a considerably different situation if I had to create many
,many Pen object but here I just create a single one.
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen blackPen = new Pen(Color.Black, 1);
if (ClientRectangle.Height / 10 > 0)
{
for (int y = 0; y < ClientRectangle.Height; y+=
ClientRectangle.Height/10)
{
g.DrawLine(blackPen, new Point(0,0), new
Point(ClientRectangle.Width,y));
}
}
}
//Tony
GC removes objects when the GC decides so because C# is managed.
At the bottom I have a code snipped.
The book I read say Always call Dispose() on Pen objects or use the using
feature on the Pen object.
I mean C# is managed so I can't understand why I should dispose the Pen
object. GC should take care of this.
The reason is probably that I mixup memory and resources. GC take care of
free memory but not resources.
I have some problem to understand why I must dispose the Pen object here
because I see the Pen object as
any object such as a Person or Car.
So my first question is can I use the rule that say always dispose an object
where the class implements the IDisposable which actually mean that I have
to check if the class implements the IDisposable ?
My second question is if I don't dispose the Pen object will a lot of
resources be allocated that will affect the application and what kind of
resources is that ?
I don't think that the amount of resources is that limited. I mean that it
would have been a considerably different situation if I had to create many
,many Pen object but here I just create a single one.
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen blackPen = new Pen(Color.Black, 1);
if (ClientRectangle.Height / 10 > 0)
{
for (int y = 0; y < ClientRectangle.Height; y+=
ClientRectangle.Height/10)
{
g.DrawLine(blackPen, new Point(0,0), new
Point(ClientRectangle.Width,y));
}
}
}
//Tony