C# is managed code where there is garbage collector responsible for memory management implicitly. Then, why we've IDisposable interface and using statement. Technically, I know use of using statement, but wonder why duplicate stuff when GC is already there?
Loading
VulpesPosted Jul 12, 2014, 10:12 AM
If a managed object uses an unmanaged resource then you have to clean that up manually when the former is no longer needed.
The IDisposable pattern was devised to make this easier for the programmer particularly when using objects in the .NET framework many of which wrap or use unmanaged resources.
If you call Dispose(), then this ensures that the unmanaged resources will be cleaned up in a timely fashion. There is normally a fallback provision whereby the destructor cleans up the unmanaged resources if Dispose() hasn't been called previously but this may take place some time later when the GC next runs and objects which have a destructor aren't destroyed immediately even then.
The using statement is just device to ensure that the Dispose method is called automatically when an object which implements IDisposable is no longer needed.
Guest UserPosted Jul 14, 2014, 8:44 AM