What's the difference, if any?

C

Carl Johansson

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
 
M

Morten Wennevik [C# MVP]

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

Hi Carl,

They are equal. The using stament is equal to try/finally and will callDispose when the scope ends.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Carl said:
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();
}
}
 

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