why do we need to implement IDisposable interface when we actually have to write the code for Dispose method. We may write code for clearing managed code without implementing the IDisposable interface? what is the point?
-
2so that you can put it in `using`, for example. And that consumers of your code have a unified way of knowing to / disposing resources. – hyankov Mar 05 '17 at 18:47
-
`IDisposable is abstraction` - we don't need it if we have proper, always updated documentation. Then we tell consumers read documentation before using our class :) – Fabio Mar 05 '17 at 18:51
-
The C# language specification requires that the type implement IDisposable to be used in a `using` statement. Why does the C# specification require that (instead of just calling a method named `Dispose`, like the `foreach` statement only requires a method named `GetEnumerator`)? That's a question only the C# language designers can definitely answer. – Michael Liu Mar 05 '17 at 18:52
-
@MichaelLiu the duck-typing of `foreach` allows for direct use of types, producing an indexer on arrays or strings, and was particularly important prior to 2.0 because there was no `IEnumerator
`. None of those advantages apply to `Dispose()`. – Jon Hanna Mar 05 '17 at 18:54 -
There is a great perf gain by having `foreach` not use `IEnumerable
` or any other interface, because `Current` and `MoveNext` can be non-virtual + not boxing is required when `GetEnumerator` returns a struct (like e.g. `List – MarcinJuraszek Mar 05 '17 at 18:57` does. None of that applies to `Dispose` + it's only called once and not potentially thousands of times for large collections. -
@JonHanna, @MarcinJuraszek: Sure. But that doesn't explain why C# doesn't **require** `foreach` types to implement IEnumerable—types with performance concerns could do so with an explicit interface implementation. Indeed, if in Jon Hanna's answer you replaced "IDisposable" with "IEnumerable" and "clean-up" with "enumeration", you'd get a good argument to make all enumerable types implement IEnumerable. – Michael Liu Mar 05 '17 at 19:25
-
@MichaelLiu yes it does. There are advantages to `foreach` that isn't through `IEnumerable` or `IEnumerable
` so those advantages are made use of (and they were greater before 2.0 so maybe things would be different if generics where in there from day one). There are no advantages to disposal that isn't through `IDisposable` so why make use of advantages that don't exist? Note further that many other uses *do* require `IEnumerable` or `IEnumerable – Jon Hanna Mar 05 '17 at 19:28`, which are usually provided alongside any duck-typed `foreach`. -
@JonHanna: What are those advantages to `foreach`, and why don't they apply to IDisposable? There is in fact a **big** disadvantage to the current C# implementation of the `using` statement: it boxes `struct` instances before calling Dispose, thereby mutating the copy instead of the original. – Michael Liu Mar 05 '17 at 19:41
-
@MichaelLiu the lesser one that remains is avoiding boxing, but the biggest one is that it allowed `Current` to return something other than `object`, which can now be done with `IEnumerator
` but that didn't exist at the time this was done. – Jon Hanna Mar 05 '17 at 21:39
3 Answers
I know the IDisposable
interface and how to use it already.
When I come to use your class, I'm going to have to learn a few things (which might be really simple, or really complicated) about what it's actually for, but I shouldn't have to learn how to do a clean-up of a sort that many other classes also do, and do through the IDisposable
interface I already know about.
That's already a big point in favour of having a well-known interface for such use. Another is that it's the best way to have code that works with lots of different types with the common feature of their requiring clean-up. I can write a method that takes IDisposable thingToDispose
a lot more easily than I can write code that cleans up different objects in different ways according to how each was implemented.

- 110,372
- 10
- 146
- 251
One major advantage of implementing IDisposable
is being able to use a using
statement. From the MSDN page on the using
statement:
The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object.
A using
statement will only accept an IDisposable
, so if you weren't to implement IDisposable
, you'd need to create your own logic to ensure Dispose
is called on your object when exiting the code block which calls methods on your object.
With IDisposable
:
using (var myObject = new MyClass()) {
... logic ...
}
Without IDisposable
:
{
var myObject = new MyClass();
try {
... logic ...
}
finally {
myObject.Dispose();
}
}
Above, the try... finally
pattern ensures that, in the case of an exception being raised, the Dispose
method is still called, which is a feature you'd have gotten by using IDisposable
in the using
. It's also in a self-contained block to ensure myObject
is not accessed elsewhere after calling Dispose
– again, a feature of the using
statement when declaring myObject
as above.

- 1,727
- 1
- 15
- 26
We may write code for clearing managed code without implementing the IDisposable interface? what is the point?
Imagine you have this class:
public class Bad
{
private StreamWriter writer;
public Bad()
{
this.writer = new StreamWriter("Bad.txt");
}
public void Write(string line)
{
writer.Write(line);
}
}
How will you write code for disposing the writer
? You cannot know from within the class when the client (consumer of your class) will be done with needing it. And the client cannot dispose it because it is private
. So how will you do it?
You can create another method called CloseWriter
like this:
public void CloseWriter()
{
this.writer.Dispose();
}
And in your documentation you can instruct clients to call this method once they are done with using your class. But the clients will need to read your documentation and sometimes, in some classes this may be called CloseWriter
and other times it may be called Done()
or Finish()
.
The whole idea of implementing the IDisposable
interface is to have a common language which all clients (developers) are familiar with. Plus they can use the using
block if you have implemented IDisposable
interface.
More Advantages
There are also more advantages.
- Class Definition
Your class definition will be like this so other developers will know right away how to properly use your class:
public class Good : IDisposable
- Many frameworks, for example, ASP MVC, WCF and others will call the
Dispose
method on your class if it implementsIDisposable
. For example, MVC will call it after the view is rendered.
You see without the interface, how will MVC know what method to call: CloseWriter()
, Done()
, Finish()
etc.
People will be able to treat your class polymorphically.
var disposables = new List(); disposables.Add(new A()); disposables.Add(new B()); disposables.Add(new C());
foreach(var thisDisposable in dispsables) { thisDisposable.Dispose(); }
That will work even if A
, B
, and C
are doing completely different things but they all implement IDisposable
interface.

- 1
- 1

- 25,467
- 4
- 62
- 64