Destructors

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

    Destructors

    Hi

    when do you use destructors and what are the advantages /disadvantages?

    ch Jim


  • Mythran

    #2
    Re: Destructors


    "Jimmy" <beffer@gmail.c om> wrote in message
    news:ObjAlrrSFH A.3332@TK2MSFTN GP15.phx.gbl...[color=blue]
    > Hi
    >
    > when do you use destructors and what are the advantages /disadvantages?
    >
    > ch Jim
    >
    >[/color]

    Use destructors anytime you need to cleanup when the class gets destroyed.
    Basically, you would implement the IDisposable interface's Dispose() method.
    Normally, cleanup includes closing any open files, closing database
    connections, cleaning up unmanaged resources, et cetera.

    HTH,
    Mythran

    Comment

    • Jimmy

      #3
      Re: Destructors

      but what are the disadvantages if any?

      ch Jim
      "Mythran" <kip_potter@hot mail.comREMOVET RAIL> wrote in message
      news:OuHF1vrSFH A.3332@TK2MSFTN GP15.phx.gbl...[color=blue]
      >
      > "Jimmy" <beffer@gmail.c om> wrote in message
      > news:ObjAlrrSFH A.3332@TK2MSFTN GP15.phx.gbl...[color=green]
      > > Hi
      > >
      > > when do you use destructors and what are the advantages /disadvantages?
      > >
      > > ch Jim
      > >
      > >[/color]
      >
      > Use destructors anytime you need to cleanup when the class gets destroyed.
      > Basically, you would implement the IDisposable interface's Dispose()[/color]
      method.[color=blue]
      > Normally, cleanup includes closing any open files, closing database
      > connections, cleaning up unmanaged resources, et cetera.
      >
      > HTH,
      > Mythran
      >[/color]


      Comment

      • Dave

        #4
        Re: Destructors

        Dispose is not a destructor.

        A disadvantage of using a Destructor is that it does not allow for explicit clean-up of an object at runtime. The Dispose method is
        a way of saying, "Hey, clean me up before you go!" to consuming classes. This allows for the explicit and timely release of
        unmanaged resources such as Database connections and File references, etc. when the consuming classes no longer require the object
        reference.

        Once "Disposed", an object should be rendered unusable but it's up to the designer to explicitly throw an ObjectDisposedE xception
        when members are accessed on a disposed custom class. You don't see this done too often :)

        If you inherit from System.Componen tModel.Componen t, you receive a base implementation that prevents the GC (Garbage Collector) from
        destroying your object without calling the virtual Dispose method first. This way, all of your cleanup code can be located in one
        place. Neat.

        I'm sure there are more disadvantages, but I can't think right now -- it's late.

        --
        Dave Sexton
        dave@www..jwaon line..com
        -----------------------------------------------------------------------
        "Jimmy" <beffer@gmail.c om> wrote in message news:eZ4ih2rSFH A.3156@TK2MSFTN GP15.phx.gbl...[color=blue]
        > but what are the disadvantages if any?
        >
        > ch Jim
        > "Mythran" <kip_potter@hot mail.comREMOVET RAIL> wrote in message
        > news:OuHF1vrSFH A.3332@TK2MSFTN GP15.phx.gbl...[color=green]
        >>
        >> "Jimmy" <beffer@gmail.c om> wrote in message
        >> news:ObjAlrrSFH A.3332@TK2MSFTN GP15.phx.gbl...[color=darkred]
        >> > Hi
        >> >
        >> > when do you use destructors and what are the advantages /disadvantages?
        >> >
        >> > ch Jim
        >> >
        >> >[/color]
        >>
        >> Use destructors anytime you need to cleanup when the class gets destroyed.
        >> Basically, you would implement the IDisposable interface's Dispose()[/color]
        > method.[color=green]
        >> Normally, cleanup includes closing any open files, closing database
        >> connections, cleaning up unmanaged resources, et cetera.
        >>
        >> HTH,
        >> Mythran
        >>[/color]
        >
        >[/color]


        Comment

        Working...