tags:

views:

54

answers:

2

below is a fragment of code that is found in my paint method. I am not sure what it is called when I create an object such as the brush this manner, but never the less will it be disposed of properly, or do I need to be concerned about it?

g.DrawString("12", _ContentFont, new SolidBrush(Color.Black), new PointF(25, 25));

vs2008,c#, .net 2.0

A: 

No. It becomes eligible for disposal. There's no guarantee on when that actually happens; it could could hang around for quite some time.

These days I would consider that a bug, though I know of time when I didn't know any better, either.

Joel Coehoorn
+5  A: 

No, it won't. Try this instead:

using ( var brush = new SolidBrush(Color.Black) )
  g.DrawString("12", _ContentFont, brush, new PointF(25, 25));

But when it comes to black, it is even better to just:

  g.DrawString("12", _ContentFont, Brushes.Black, new PointF(25, 25));
danbystrom