Question about IDisposable

My question is: why do I need IDisposable? I have a class that consumes some resources that need to be freed. I have two options

Without IDisposable

class SomeUtilityClass { public Stop() { // free resources } } 

With IDisposable

  class SomeUtilityClass, IDisposable { public void Dispose() { // free resources } } 

So why do I need IDisposable here? Unable to name function.

 class Program { public Main(..) { SomeUtilityClass _class = new SomeUtilityClass(); // clean up when we've done _class.Stop(); // OR _class.Dispose(); } } 
+4
source share
7 answers

Since IDisposable is supported by C #, and you can use the cool using syntax:

 using (IDisposable someDisposable = new ...) { // your code with someDisposable } 

This is actually converted by the compiler into something like:

 IDisposable someDisposable = new ... IDisposable someDisposable2 = someDisposable ; try { // your code with someDisposable } finally { if (someDisposable2 != null) { someDisposable2.Dispose(); } } 

So, if any exception occurs inside the using block, your object will still be deleted.

+5
source

You really should use IDisposable when your class consumes unmanaged resources and they need to be freed immediately (streams, db, etc.).

It also provides the ability for the CLR to β€œclear” when there are unhandled exceptions that cause thread unloading.

Calling IDisposable immediately puts the object as available for garbage collection, but if it is not implemented correctly, you can force your object to promote garbage collection, which can cause memory pressure (refer to Jeffery Richters CLR via C # 3 if you want a full explanation) .

quick google changed this: http://kleahy-technical.blogspot.com/2009/01/idisposable-and-garbage-collection.html

I suggest you read the IDisposable template when to use it, when it is not, and its implications for the GC and state.

EDIT: there is also a lot of information on stackoverflow: Using garbage collection?

+3
source

In your case, IDisposable is not implemented as much, as you can manually manage your resources.

The usual use of IDisposable is when you open an interface that handles data connections, and you want all the derived classes to be located when they were made.

Consider this:

 public interface IDataCtx { void CallDB(); } public class MyDataCtx : IDataCtx { private SqlConnection dc; public MyDataCtx() { dc = new SqlConnection(); dc.Open(); } public void CallDB(); { dc.Something(); } } 

Lets you do something like this:

 IDataCtx ctx = new MyDataCtx(); ctx.CallDB(); 

But wait, what about this open connection? Oh!

If you made IDataCtx : IDisposable (and implemented the code in your derived ctx), you can do this:

 IDataCtx ctx; using (ctx = new MyDataCtx()) { ctx.CallDB(); } 

Ensuring that any implementation of IDataCtx that you use will always be deleted (even in the event of an exception).

That I use it anyway. (plus it's just good practice).

+2
source

IDisposable interacts with the using keyword to simplify cleaning after itself, for example:

 using (var file = new FileStream(...)) { file.Write(...); } 

In the above code, the FileStream closes as soon as the use block completes, rather than waiting to collect garbage.

+1
source

This is the convention used in C #.

You will also get excellent using instructions.

 using (SomeUtilityClass _class = new SomeUtilityClass()) { } // Dispose is automatically called 
+1
source

In addition to being able to use the using statement, it also gives the garbage collector a hint that the object can be deleted from memory.

+1
source

If your class owns unmanaged resources or your class owns IDisposable managed resources, you must embed the IDisposable interface.

An easy-to-read short article on when to implement IDisposable and Finalizers can be found here: http://nitoprograms.blogspot.com/2009/08/how-to-implement-idisposable-and.html

+1
source

All Articles