I have a large project that utilises inline declared objects for one time use (brushes, colors, fonts etc).
when i run the code analysis tool in VS2010 I am warned that there are objects that dont get disposed on every path.
given the line of code below how can I ensure that the items raised are disposed explicitly when no longer in use or when an exception occurs.
g.DrawString(stringNames[i],
(Font)new Font("Segoe UI", 7, FontStyle.Regular),
(Brush)Brushes.Black,
new Point(hStart.X - 12, hStart.Y - 6));
thanks in advance
You can ensure your
Graphicsobject gets immediately disposed after use by wrapping it inside of ausingstatement. The same goes for any object implementingIDisposable.As a side note, you don’t need to explicitly cast the Font or Brush object in your example above. These are already strongly typed.