You still have to dispose it when you're done.
For example, you could call it like this:
using (Pen p = CreatePenFromColor(color))
{
// do something
}
If a method returns an IDisposable object, it is your duty to dispose it.
[Edit] Now I got the question -- you are using the Pen(Brush b) constructor.
a. In this case, it seems that Pen does not need the Brush instance after constructor, so your method could look like this:
public Pen CreatePenFromColor(Color c)
{
using (Brush b = new SolidBrush(c))
{ return new Pen(b); }
}
b. Why not simply use Pen(Color color)?
public Pen CreatePenFromColor(Color c)
{
return new Pen(c);
}
c. (regarding the comment) If the Pen would hold a reference to the Brush internally, then you wouldn't be able to dispose it before you are finished with the Pen. In that case, I would go for a class which would do the job for me:
public class PenHelper : IDisposable
{
private readonly Brush _brush;
public PenHelper(Color color)
{
_brush = new SolidBrush(color);
}
public Pen CreatePen()
{
return new Pen(_brush);
}
public void Dispose()
{
_brush.Dispose();
}
}
and then use it like this:
using (PenHelper penHelper = new PenHelper(Color.Black))
{
using (Pen pen = penHelper.CreatePen())
{
// do stuff
}
}
Disclaimer: IDisposable is not implemented according to guidelines, but rather for demonstration only. Also, the whole example is used only to show how to encapsulate a reference when needed. You should go for Pen(color) of course.