Dispose

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

    Dispose

    Hi

    can anyone point to a good link explaining (simply) when I should use the
    dispose method.

    Sort of things I'd like to know are

    how does it differ from finalize in VB.net (or is it nothing like finalize)
    Does the garbage collector automatically call the dispose method when an
    object goes out of scope.
    Does every object support it, if not, which do
    Does it have to be called manually for every object that does support it
    ....

    this sort of thing

    thanks

    Andy


  • Vijaye Raji

    #2
    Re: Dispose

    1. Finalize is different from Dispose. Finalize is like a destructor, while
    Dispose is like a Free() call (sort of)
    2. The garbage collector will not call the Dispose method
    3. Not every object supports Dispose. Any class that implements IDisposable
    will support it.
    4. Yes, you need to call it manually (or use the "using" keyword, in which
    case the compiler will generate code to call Dispose)

    Dispose is where you would free up any resource that you are hanging on to.
    That would be a good place to close files or close any Database connections
    you have open. Not every object needs it.

    -vj

    "aaj" <aaj@aaj.com> wrote in message
    news:1115968104 .9b46c80adea689 3baec8ff88f2232 3aa@teranews...[color=blue]
    > Hi
    >
    > can anyone point to a good link explaining (simply) when I should use the
    > dispose method.
    >
    > Sort of things I'd like to know are
    >
    > how does it differ from finalize in VB.net (or is it nothing like
    > finalize)
    > Does the garbage collector automatically call the dispose method when an
    > object goes out of scope.
    > Does every object support it, if not, which do
    > Does it have to be called manually for every object that does support it
    > ...
    >
    > this sort of thing
    >
    > thanks
    >
    > Andy
    >
    >[/color]


    Comment

    • Michael S

      #3
      Re: Dispose


      "aaj" <aaj@aaj.com> wrote in message
      news:1115968104 .9b46c80adea689 3baec8ff88f2232 3aa@teranews...[color=blue]
      > Hi[/color]

      Hi
      [color=blue]
      > can anyone point to a good link explaining (simply) when I should use the
      > dispose method.[/color]

      Ther are plenty Just google.
      [color=blue]
      > Sort of things I'd like to know are
      >
      > how does it differ from finalize in VB.net (or is it nothing like
      > finalize)[/color]
      - It is nothing like finalize.
      [color=blue]
      > Does the garbage collector automatically call the dispose method when an
      > object goes out of scope.[/color]
      - Nope. You have to do that yourself. Have a look at the using keyword.
      [color=blue]
      > Does every object support it, if not, which do[/color]
      - Typically classes that uses external resources like SqlConnection and
      StreamReader. FxCop is a nifty tool that will tell you if you missed calling
      Dispose on such objects.
      [color=blue]
      > Does it have to be called manually for every object that does support it[/color]
      - Yep. (again see the using keyword)
      [color=blue]
      > thanks
      >
      > Andy[/color]

      Andy, how the GC works, and what makes finalizers in .NET so very different
      from a destructor in unmanaged runtimes like C++ and Delphi; is of grave
      importance. You really should take a break from coding and start reading all
      about it. This will teach you why we need the Dispose pattern in .NET.

      Happy Coding, err Reading...
      - Michael S


      Comment

      • Bob Powell [MVP]

        #4
        Re: Dispose

        My philosophy is that if an object is disposable it's probably for a reason.
        If Dispose exists, use it when your done with the object.

        Only objects that implement IDisposable will have Dispose methods.

        Finalize may call Dispose but that isn't certain and will depend upon the
        person that wrote the object.

        --
        Bob Powell [MVP]
        Visual C#, System.Drawing

        Find great Windows Forms articles in Windows Forms Tips and Tricks


        Answer those GDI+ questions with the GDI+ FAQ


        All new articles provide code in C# and VB.NET.
        Subscribe to the RSS feeds provided and never miss a new article.





        "aaj" <aaj@aaj.com> wrote in message
        news:1115968104 .9b46c80adea689 3baec8ff88f2232 3aa@teranews...[color=blue]
        > Hi
        >
        > can anyone point to a good link explaining (simply) when I should use the
        > dispose method.
        >
        > Sort of things I'd like to know are
        >
        > how does it differ from finalize in VB.net (or is it nothing like
        > finalize)
        > Does the garbage collector automatically call the dispose method when an
        > object goes out of scope.
        > Does every object support it, if not, which do
        > Does it have to be called manually for every object that does support it
        > ...
        >
        > this sort of thing
        >
        > thanks
        >
        > Andy
        >
        >[/color]


        Comment

        • Cor Ligthert

          #5
          Re: Dispose

          Aaj,

          See this message from Jay B. which describes it the best I have seen until
          now.



          I hope this helps,

          Cor


          Comment

          Working...