-5

I want to know suppose I create an object and then use (IDisposable(object)).Dispose() after using that object.

Will this help me to improve my performance?Or should I wait for GarbageCollector to collect that object?

Sateesh Pagolu
  • 9,282
  • 2
  • 30
  • 48
vishu9219
  • 761
  • 6
  • 14

2 Answers2

1

The primary use of this interface is to release unmanaged resources. The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used. However, it is not possible to predict when garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams. Use the Dispose method of this interface to explicitly release unmanaged resources in conjunction with the garbage collector. The consumer of an object can call this method when the object is no longer needed

ref: http://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx

A more detailed description http://www.codeproject.com/Articles/413887/Understanding-and-Implementing-IDisposable-Interfa

Nipun Ambastha
  • 2,553
  • 1
  • 16
  • 26
  • The primary use of `IDisposable` is to release **resources**, no matter what nature those resources have. – Dennis Mar 11 '14 at 08:14
0

Its purely depends on the context which we used, for example objects like DB connection objects usually needs to be disposed, any stream related operations needs to be disposed. It's not necessary to dispose every object which we create, most of them are handled by GC.

Venkat
  • 115
  • 2
  • 14