Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Dispsose() or Close()

Dispsose() or Close()

Scheduled Pinned Locked Moved C#
6 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TJS4u
    wrote on last edited by
    #1

    hello all which of the follwing is safer to use this.Dispose() OR this.Close() Wht is the fundamental difference btwn these two Thanks TJS

    S L E G C 5 Replies Last reply
    0
    • T TJS4u

      hello all which of the follwing is safer to use this.Dispose() OR this.Close() Wht is the fundamental difference btwn these two Thanks TJS

      S Offline
      S Offline
      Simon P Stevens
      wrote on last edited by
      #2

      TJS4u wrote:

      this.

      Well it depends what Type "this" is. In some cases Streams/IO and such, they are fairly comparable. Dispose will normally call close. If an object is disposable you should always call dispose when you are finished with it to ensure the finaliser is suppressed. Normally it's ok to just call dispose, but there is no harm in closing explicitly first just to be safe. For some types of objects, like Forms, close has a specific meaning. Close() will close the form. Dispose will probably also call close, but in many cases you may want to close the form, but still keep the object to do some work with before you are ready to dispose of it.

      Simon

      1 Reply Last reply
      0
      • T TJS4u

        hello all which of the follwing is safer to use this.Dispose() OR this.Close() Wht is the fundamental difference btwn these two Thanks TJS

        L Offline
        L Offline
        Lev Danielyan
        wrote on last edited by
        #3

        Close() might eventually call Dispose(), actually depends on the class implementation, whereas Dispose must be defined if IDisposable is implemented for resource cleanup Use using clause; using(resource) { } This will automatically dispose your resource (say a stream), if it implements IDisposable interface

        Regards, Lev

        1 Reply Last reply
        0
        • T TJS4u

          hello all which of the follwing is safer to use this.Dispose() OR this.Close() Wht is the fundamental difference btwn these two Thanks TJS

          E Offline
          E Offline
          Expert Coming
          wrote on last edited by
          #4

          Close and Dispose for most classes seem to be very close. And in many cases Dispose will call Close. But, the fundamental difference is that Close has the class Dispose alot of its objects, while Dispose actually Disposes the object you are calling it on.

          The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo

          1 Reply Last reply
          0
          • T TJS4u

            hello all which of the follwing is safer to use this.Dispose() OR this.Close() Wht is the fundamental difference btwn these two Thanks TJS

            G Offline
            G Offline
            Giorgi Dalakishvili
            wrote on last edited by
            #5

            As it was already said it depends on actual type. For example with sqlconnection object close() and dispose() do the same thing. For other types you can use Reflector to see what dispose() and close() do and compare them.

            Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

            1 Reply Last reply
            0
            • T TJS4u

              hello all which of the follwing is safer to use this.Dispose() OR this.Close() Wht is the fundamental difference btwn these two Thanks TJS

              C Offline
              C Offline
              carbon_golem
              wrote on last edited by
              #6

              To find the difference between the two in a specific instance, .NET Reflector on the type you'd like to examine. You can download it for free from redgate.com. Scott P

              "Simplicity carried to the extreme becomes elegance."
              -Jon Franklin

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups