Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7444239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:28:14+00:00 2026-05-29T11:28:14+00:00

While writing a small paint-like application (for myself), I originally had the following code

  • 0

While writing a small paint-like application (for myself), I originally had the following code being called in the onClick handler:

g.DrawEllipse((new Pen(pencolour, penSize)), e.X, e.Y, 1, 1);

which I later changed to

Pen pen1 = new Pen(pencolour, penSize);
g.DrawEllipse(pen1, e.X, e.Y, 1, 1);
pen1.Dispose();

My question is: are the two pieces of code equivalent, or does the first one create Pen objects which are never disposed of?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:28:17+00:00Added an answer on May 29, 2026 at 11:28 am

    They are disposed when the Garbage Collector runs and determines the object is no longer in use. It is better to dispose of objects yourself, so the resources are freed immediately.

    Also consider the use of the using statement:

    using (Pen pen1 = new Pen(pencolour, penSize))
    {
        g.DrawEllipse(pen1, e.X, e.Y, 1, 1);
    }
    

    This automatically disposes the Pen, even if DrawEllipse would throw an exception, and the IDE will enforce that pen1 is only available from within the using block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While writing some C code, I decided to compile it to assembly and read
While writing code in a file that would comprise of PHP, HTML, CSS &
While writing code, it is pretty common to request a page with an appended
Sometimes while writing Java in Eclipse, I write code that generates warnings. A common
I got this doubt while writing some code. Is 'bool' a basic datatype defined
I have been writing a small java application (my first!), that does only a
I'm writing unit tests for a module in a small Javascript application. In order
I'm writing a very small application for my organization. As nobody seems to know
I'm writing a small program with an interpreter, I would like to pipe any
I'm working on learning TDD while writing some small ruby programs. I have the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.