Using keyword and IDisposable

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

    Using keyword and IDisposable

    Hi all,

    I currenty have a datalayer and have decided to impliment IDisposable
    with it. The data layer contains a number of objects which I can call
    dispose on, but i'm not too sure if you need to worry about strings.
    In the dispose method, is it best to set these to "", or set them to
    null? Also, I have an List<Tobject, should this be cleared and set
    to null?
    I usually clean things up myself, but figured for a datalayer,
    IDisposable and using{} might be an extra safeguard.

    Any help with this would be appreciated.

    Paul
  • Alvin Bruney [ASP.NET MVP]

    #2
    Re: Using keyword and IDisposable

    idisposable isn't required unless you have special circumstances for
    cleaning up objects. Strings are handled automatically. Setting a string to
    empty or null does nothing for you and in fact prolongs the life of the
    string by the extra references. If you intend your data layer to collect
    unmanaged objects at some point in its lifetime or requires care in
    destroying the objects, definitely implement idisposable. I understand some
    people simply like to implement idisposable because it is recommended. I
    think that's ok too as long as you understand its impact on your code.

    --
    Regards,
    Alvin Bruney

    Want a free copy of VS 2008 w/ MSDN premium subscription?
    Details at http://msmvps.com/blogs/alvin/Default.aspx

    Auther Plug
    OWC Blackbook now on download at www.lulu.com/owc

    "Paul" <Gef.Mongoose@g mail.comwrote in message
    news:78493c76-edaf-47f8-b969-c9c935a2fef5@z7 2g2000hsb.googl egroups.com...
    Hi all,
    >
    I currenty have a datalayer and have decided to impliment IDisposable
    with it. The data layer contains a number of objects which I can call
    dispose on, but i'm not too sure if you need to worry about strings.
    In the dispose method, is it best to set these to "", or set them to
    null? Also, I have an List<Tobject, should this be cleared and set
    to null?
    I usually clean things up myself, but figured for a datalayer,
    IDisposable and using{} might be an extra safeguard.
    >
    Any help with this would be appreciated.
    >
    Paul

    Comment

    • Paul

      #3
      Re: Using keyword and IDisposable

      On 7 Oct, 12:09, "Alvin Bruney [ASP.NET MVP]" <vapor dan a t h u t ma
      le dut cu mwrote:
      idisposable isn't required unless you have special circumstances for
      cleaning up objects. Strings are handled automatically. Setting a string to
      empty or null does nothing for you and in fact prolongs the life of the
      string by the extra references. If you intend your data layer to collect
      unmanaged objects at some point in its lifetime or requires care in
      destroying the objects, definitely implement idisposable. I understand some
      people simply like to implement idisposable because it is recommended. I
      think that's ok too as long as you understand its impact on your code.
      >
      --
      Regards,
      Alvin Bruney
      >
      Want a free copy of VS 2008 w/ MSDN premium subscription?
      Details athttp://msmvps.com/blogs/alvin/Default.aspx
      >
      Auther Plug
      OWC Blackbook now on download atwww.lulu.com/owc
      >
      "Paul" <Gef.Mongo...@g mail.comwrote in message
      >
      news:78493c76-edaf-47f8-b969-c9c935a2fef5@z7 2g2000hsb.googl egroups.com...
      >
      >
      >
      Hi all,
      >
      I currenty have a datalayer and have decided to impliment IDisposable
      with it. The data layer contains a number of objects which I can call
      dispose on, but i'm not too sure if you need to worry about strings.
      In the dispose method, is it best to set these to "", or set them to
      null? Also, I have an List<Tobject, should this be cleared and set
      to null?
      I usually clean things up myself, but figured for a datalayer,
      IDisposable and using{} might be an extra safeguard.
      >
      Any help with this would be appreciated.
      >
      Paul- Hide quoted text -
      >
      - Show quoted text -
      Thanks for the reply Alvin. The datalayer seems to do a pretty good
      job of closing connections and cleaning up after itself (I haven't had
      any issues yet), but recently I read a blog regarding someone
      implimenting IDisposable in their DB layer and seeing memory usage
      reduced by up to 40% - so thought i'd look into it. Does implementing
      it have a performance hit? If so, i'll just stick to the way I was
      doing my clean up before.

      Thanks for the help.

      Comment

      • =?Utf-8?B?YnJ1Y2UgYmFya2Vy?=

        #4
        RE: Using keyword and IDisposable

        a class implements idisposable if it uses unmanged resources, and wants them
        released as soon as possible, not when the GC get around to it. if a class
        implements idisposable it should always be called. better yet use a using
        statement. database connections, and commands need to be disposed. if your
        code (as it should) always disposes these object before returning from any
        method, then your class does not need to implement idispose.

        there is a cost to calling idispose, as it forces a GC run on the current
        thread rather than the background thread.

        List<does not implement IDispose as it does not need it. but if you insert
        objects that require disposing, your code needs to loop thru the list and
        dispose the objects when you are done with the list<>

        -- bruce (sqlwork.com)


        "Paul" wrote:
        Hi all,
        >
        I currenty have a datalayer and have decided to impliment IDisposable
        with it. The data layer contains a number of objects which I can call
        dispose on, but i'm not too sure if you need to worry about strings.
        In the dispose method, is it best to set these to "", or set them to
        null? Also, I have an List<Tobject, should this be cleared and set
        to null?
        I usually clean things up myself, but figured for a datalayer,
        IDisposable and using{} might be an extra safeguard.
        >
        Any help with this would be appreciated.
        >
        Paul
        >

        Comment

        Working...