I have some classes which have Fonts and Brushes as private fields. Something says me this isn’t correct practice, and should be avoided, yet I don’t know why …. (Darn subconsiousness 🙂 )
Is this true, and why is it bad practice ?
class SomeControl: Panel
{
private Font titelFont = new Font("Arial", 8.25f, FontStyle.Bold);
private SolidBrush whiteBrush = new SolidBrush(Color.White);
public SomeControl()
{
//Do stuff
}
}
I don’t see why it’s bad.
When using brushes, you already have static
Brushesclass you may want to reuse instead of creating your own (just like you do with common colors).For example, instead of creating
whiteBrush, you may rather useBrushes.Whitedirectly.Of course, it gives you only a common set of brushes, so you still need to create your own brush for any other color.
Just remember than fonts and brushes must be properly disposed. It means that when using your own fonts and brushes as a property or a field of a class, this class must implement
IDisposableand properly dispose those fonts and brushes.