[dba-VB] C#: When to Dispose

Gustav Brock Gustav at cactus.dk
Fri Aug 29 04:46:23 CDT 2008


Hi Charlotte

And, as I noticed the other day, if you look up the .designer.cs code for any form created from the VS template, this is found (probably similar for VB.NET) which may tell the form to clean up its components:

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

So no magic, just handcraft.

/gustav

>>> cfoust at infostatsystems.com 29-08-2008 02:03 >>>
That's the way I understand it.

Charlotte Foust 

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com 
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: Wednesday, August 27, 2008 12:42 AM
To: dba-vb at databaseadvisors.com 
Subject: Re: [dba-VB] C#: When to Dispose

Hi Charlotte

That makes sense. So, for example, all the common stuff used by a form
is disposed automatically when the form is closed. And when I use the
e-mail components I can manually dispose those when done to free VS to
keep track of that.

/gustav

>>> cfoust at infostatsystems.com 26-08-2008 01:15 >>>
It was more important in earlier version of VS because the garbage
collection wasn't as good as the current versions.  If you do dispose an
object, make sure you aren't going to need it again in a minute, because
it's gone!

Charlotte Foust 

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com 
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: Friday, August 22, 2008 9:27 AM
To: dba-vb at databaseadvisors.com 
Subject: [dba-VB] C#: When to Dispose

Hi all

Came across this article on the subject:

  http://www.devx.com/dotnet/Article/33167 

and realise that I don't dispose objects that much and yet the code runs
- and most of the coding examples I see don't tell much about it.

Right now I'm writing some code to send a series of e-mails and the code
I found calls Dispose. That makes sense: Create the MailMessage object,
send the message, dispose object.

But what experience do you have? How keen are you about disposing
objects?

/gustav






More information about the dba-VB mailing list