[quoted text, click to view] Carl Johansson wrote:
> Except for the syntax, what is the difference, if any, between this...
>
> /* ---------------------------------------------------------------------- */
> private void Form1_Paint(object sender, PaintEventArgs e)
> {
> Pen pen = null;
>
> try
> {
> pen = new Pen(Color.MidnightBlue, 1.0F);
> e.Graphics.DrawLine(pen, new Point(20, 270), new Point(20, 10));
> }
> finally
> {
> if (pen != null)
> pen.Dispose();
> }
> }
> /* ---------------------------------------------------------------------- */
>
> and this...
>
> /* ---------------------------------------------------------------------- */
> private void Form1_Paint(object sender, PaintEventArgs e)
> {
> using (Pen pen = new Pen(Color.MidnightBlue, 1.0F))
> e.Graphics.DrawLine(pen, new Point(20, 270), new Point(20, 10));
> }
> /* ---------------------------------------------------------------------- */
>
> ?
>
> Regards Carl Johansson
>
This code:
Pen pen = null;
try {
pen = new Pen(Color.MidnightBlue, 1.0F);
e.Graphics.DrawLine(pen, new Point(20, 270), new Point(20, 10));
} finally {
if (pen != null) {
pen.Dispose();
}
}
is the exact equivalent of:
using (Pen pen = null) {
pen = new Pen(Color.MidnightBlue, 1.0F);
e.Graphics.DrawLine(pen, new Point(20, 270), new Point(20, 10));
}
This code:
using (Pen pen = new Pen(Color.MidnightBlue, 1.0F)) {
e.Graphics.DrawLine(pen, new Point(20, 270), new Point(20, 10));
}
is the exact equivalent of:
Pen pen = new Pen(Color.MidnightBlue, 1.0F);
try {
e.Graphics.DrawLine(pen, new Point(20, 270), new Point(20, 10));
} finally {
if (pen != null) {
pen.Dispose();
}
}
--
Göran Andersson
_____