What's the difference, if any?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carl Johansson

    #1

    What's the difference, if any?

    Except for the syntax, what is the difference, if any, between this...

    /* ---------------------------------------------------------------------- */
    private void Form1_Paint(obj ect sender, PaintEventArgs e)
    {
    Pen pen = null;

    try
    {
    pen = new Pen(Color.Midni ghtBlue, 1.0F);
    e.Graphics.Draw Line(pen, new Point(20, 270), new Point(20, 10));
    }
    finally
    {
    if (pen != null)
    pen.Dispose();
    }
    }
    /* ---------------------------------------------------------------------- */

    and this...

    /* ---------------------------------------------------------------------- */
    private void Form1_Paint(obj ect sender, PaintEventArgs e)
    {
    using (Pen pen = new Pen(Color.Midni ghtBlue, 1.0F))
    e.Graphics.Draw Line(pen, new Point(20, 270), new Point(20, 10));
    }
    /* ---------------------------------------------------------------------- */

    ?

    Regards Carl Johansson





  • Morten Wennevik [C# MVP]

    #2
    Re: What's the difference, if any?

    On Sat, 01 Sep 2007 18:41:35 +0200, Carl Johansson <carl.johansson @nogarbagehalld e.comwrote:
    Except for the syntax, what is the difference, if any, between this...
    >
    /* ---------------------------------------------------------------------- */
    private void Form1_Paint(obj ect sender, PaintEventArgs e)
    {
    Pen pen = null;
    >
    try
    {
    pen = new Pen(Color.Midni ghtBlue, 1.0F);
    e.Graphics.Draw Line(pen, new Point(20, 270), new Point(20, 10));
    }
    finally
    {
    if (pen != null)
    pen.Dispose();
    }
    }
    /* ---------------------------------------------------------------------- */
    >
    and this...
    >
    /* ---------------------------------------------------------------------- */
    private void Form1_Paint(obj ect sender, PaintEventArgs e)
    {
    using (Pen pen = new Pen(Color.Midni ghtBlue, 1.0F))
    e.Graphics.Draw Line(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.


    --
    Happy coding!
    Morten Wennevik [C# MVP]

    Comment

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

      #3
      Re: What's the difference, if any?

      Carl Johansson wrote:
      Except for the syntax, what is the difference, if any, between this...
      >
      /* ---------------------------------------------------------------------- */
      private void Form1_Paint(obj ect sender, PaintEventArgs e)
      {
      Pen pen = null;
      >
      try
      {
      pen = new Pen(Color.Midni ghtBlue, 1.0F);
      e.Graphics.Draw Line(pen, new Point(20, 270), new Point(20, 10));
      }
      finally
      {
      if (pen != null)
      pen.Dispose();
      }
      }
      /* ---------------------------------------------------------------------- */
      >
      and this...
      >
      /* ---------------------------------------------------------------------- */
      private void Form1_Paint(obj ect sender, PaintEventArgs e)
      {
      using (Pen pen = new Pen(Color.Midni ghtBlue, 1.0F))
      e.Graphics.Draw Line(pen, new Point(20, 270), new Point(20, 10));
      }
      /* ---------------------------------------------------------------------- */
      >
      ?
      >
      Regards Carl Johansson
      >
      This code:

      Pen pen = null;
      try {
      pen = new Pen(Color.Midni ghtBlue, 1.0F);
      e.Graphics.Draw Line(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.Midni ghtBlue, 1.0F);
      e.Graphics.Draw Line(pen, new Point(20, 270), new Point(20, 10));
      }


      This code:

      using (Pen pen = new Pen(Color.Midni ghtBlue, 1.0F)) {
      e.Graphics.Draw Line(pen, new Point(20, 270), new Point(20, 10));
      }

      is the exact equivalent of:

      Pen pen = new Pen(Color.Midni ghtBlue, 1.0F);
      try {
      e.Graphics.Draw Line(pen, new Point(20, 270), new Point(20, 10));
      } finally {
      if (pen != null) {
      pen.Dispose();
      }
      }

      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      Working...