The Dispose
method is a standard method defined in the IDisposable
interface. It’s used to release unmanaged resources such as file handles, database connections, or unmanaged memory used by an object. Objects that allocate such resources should implement IDisposable
and provide an implementation for Dispose
.
Just go through following questions to understand the Dispose and Using in depth. Some of the answers are redundant to emphasize the point and make it absolutely clear to the reader.
Who calls Dispose Method ?
You (as developer) need to call the Dispose method. It is not called automatically by .Net run time or Garbage collector for that matter. You can implicitly call dispose by using “Using” block.
If you use “Using” statement like Using (var obj =new SomeDisposableObject()
) , you are ensuring that once the using block ends it will get called.
When you use “Using” block, internally C# compiler compiles this into a try-finally
block behind the scenes.
For e.g. this lines of code
![](https://eazyreads.com/wp-content/uploads/2024/07/image-1.png)
gets translated into
![](https://eazyreads.com/wp-content/uploads/2024/07/image-2.png)
All the codes inside the using block will go inside the try block. Finally will ensure that even if there is any error while executing the code inside “Using” it will call the Dispose method.
Can I use “Using” block with any class ?
No. Only class that implements IDisposable can be called using “Using” block.
If you are writing your own class that uses any unmanaged code, you need to implement IDisposable yourself and implement your own Dispose method for it to be called within using block.
Some of the common Classes in .Net library that implements IDisposable and thus can be used with “Using”
- FileStream
- MemoryStream
- SqlConnection
- SqlCommand
- WebClient
- HttpClient
If you write your own custom class like “MyClass” without implement IDisposable, you won’t be able to use “Using” as it doesn’t implement IDisposable.
Why should I call Dispose method or use ‘Using’ block?
To free up unmanaged resources only, Dispose needs to be called. All managed resources are cleaned up by Garbage collector but Garbage Collector doesn’t understand unmanaged resource and thus leaves it to the developer to dispose/clear the memory themselves.
Why don’t you call Dispose (or use ‘Using’) for all the classes you create?
Garbage collector can handle managed resources (resources that are understood by .Net runtime), so GC clears any memory that are no more in use by itself and we don’t need to bother about them. All unmanaged resources like file handling, web client, database connection etc is not managed by Garbage collector and hence Devs need to explicitly clean up by calling their Dispose method.
How do I know if I need to call Dispose method or use Using block?
Just check if the class implement’s IDisposable method. If you try to use “Using” for any other class it will give you compiler error and you will not be able to build . Just to make it clear if you are using “Using” you don’t need to call Dispose method explicitly.