Do I need to dispose brush explicitly?

G

Guest

Hi all

In following code, is it OK not to dispose local brush explicitly before
exiting the procedure?

private void MyDrawItem ( Graphics g, Color BackColor, Rectangle r )
{
g.FillRectangle(new SolidBrush(backColor), r);
}

Thanks
Alex
 
N

Nicholas Paldino [.NET/C# MVP]

Alex,

Yes, you do. You should be doing this:

private void MyDrawItem ( Graphics g, Color BackColor, Rectangle r )
{
using (SolidBrush solidBrush = new SolidBrush(backColor))
{
g.FillRectangle(solidBrush, r);
}
}

Hope this helps.
 
B

Bob Powell [MVP]

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top