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. Should I use Dispose in this situation

Should I use Dispose in this situation

Scheduled Pinned Locked Moved C#
csharptutorialquestion
4 Posts 4 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.
  • X Offline
    X Offline
    xkx32
    wrote on last edited by
    #1

    hello, i'm initiating an object inside a method: private void Example () { MyClass ins = new MyClass(...); ins.AMethod(); ins.Dispose(); } i'm wondering if the last line: ins.Dispose(); is necessary? Or C# does it automaticlly?... i mean disposing the object as soon as the method is finish. please advise

    C G L 3 Replies Last reply
    0
    • X xkx32

      hello, i'm initiating an object inside a method: private void Example () { MyClass ins = new MyClass(...); ins.AMethod(); ins.Dispose(); } i'm wondering if the last line: ins.Dispose(); is necessary? Or C# does it automaticlly?... i mean disposing the object as soon as the method is finish. please advise

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      xkx32 wrote:

      i'm wondering if the last line: ins.Dispose(); is necessary? Or C# does it automaticlly?...

      To the first question - If the class has a Dispose() method then, yes, you should use it. To the second question - If the class has a destructor (Finalizer) that calls Dispose() then, yes, it will eventually get to it. BUT, just because it will get to it eventually doesn't mean you can get all lazy about it. If a class supports Dispose() it is usually for a very good reason and its very existance suggests that you should use it.

      xkx32 wrote:

      i mean disposing the object as soon as the method is finish

      If that is where the object is naturally no longer required then that is where you should dispose of it. If you are only going to use an object within one method and it won't be stored anywhere else by the time the method completed then you may like to use the following instead

      using (MyClass ins = new MyClass())
      {
      ins.AMethod();
      }

      What happens here is that as soon as the code exits the scope of the using block for what ever reason (it might reach it naturally, or it might be in the process of throwing an exception) the object will be Disposed of.


      Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

      1 Reply Last reply
      0
      • X xkx32

        hello, i'm initiating an object inside a method: private void Example () { MyClass ins = new MyClass(...); ins.AMethod(); ins.Dispose(); } i'm wondering if the last line: ins.Dispose(); is necessary? Or C# does it automaticlly?... i mean disposing the object as soon as the method is finish. please advise

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        The Dispose method doesn't dispose of the object itself, it tells the object to dispose of the unmanaged resources that it's using. If an object has a Dispose method, you should use it. It helps the object to free resources as soon as possible. If you don't call Dispose, you won't know when the resources will be freed. As .NET uses garbage collection, not reference counting, the object will not be finalized immediately when there is no reference to it, but when the garbage collector does the next collection. Depending on the memory "turnover" of the program, this can be in seconds, minutes, hours... or not until the application ends.

        --- b { font-weight: normal; }

        1 Reply Last reply
        0
        • X xkx32

          hello, i'm initiating an object inside a method: private void Example () { MyClass ins = new MyClass(...); ins.AMethod(); ins.Dispose(); } i'm wondering if the last line: ins.Dispose(); is necessary? Or C# does it automaticlly?... i mean disposing the object as soon as the method is finish. please advise

          L Offline
          L Offline
          LongRange Shooter
          wrote on last edited by
          #4

          Microsoft technicians state that you should never call Dispose and always allow the Garbage Collector to handle object management. That object exposing the IDispose method places it at the top of the list for GC to process.

          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