Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

IDisposable

2 views
Skip to first unread message

Tony Johansson

unread,
Feb 25, 2011, 1:26:30 PM2/25/11
to
Hello!

I know what this interface is but I have never had any reason to implement
this interface because the .NET framework already implementing this
interface. So I ask if somebody can give an exampel of when it would be
suitable to implement this interface.?


//Tony


Arne Vajhøj

unread,
Feb 25, 2011, 1:56:43 PM2/25/11
to

You should implement it every time you have a class that
keeps unmanaged resources (open files, open database
connections, open network connections) at the instance
level to ensure that those resources get properly released.

Arne


Tony Johansson

unread,
Feb 25, 2011, 3:54:19 PM2/25/11
to

"Arne Vajhøj" <ar...@vajhoej.dk> skrev i meddelandet
news:4d67fb68$0$23754$1472...@news.sunsite.dk...

But .NET itself do implement this IDisposable so why should I then implemet
it in my class ?

//Tony


Arne Vajhøj

unread,
Feb 25, 2011, 4:29:29 PM2/25/11
to
On 25-02-2011 15:54, Tony Johansson wrote:
> "Arne Vajhøj"<ar...@vajhoej.dk> skrev i meddelandet
> news:4d67fb68$0$23754$1472...@news.sunsite.dk...
>> On 25-02-2011 13:26, Tony Johansson wrote:
>>> I know what this interface is but I have never had any reason to
>>> implement
>>> this interface because the .NET framework already implementing this
>>> interface. So I ask if somebody can give an exampel of when it would be
>>> suitable to implement this interface.?
>>
>> You should implement it every time you have a class that
>> keeps unmanaged resources (open files, open database
>> connections, open network connections) at the instance
>> level to ensure that those resources get properly released.
>
> But .NET itself do implement this IDisposable so why should I then implemet
> it in my class ?

Let me give an example.

public class Foo
{
public void M(string fnm)
{
using(StreamReader sr = new StreamReader(fnm))
{
// read lines and do something with them
}
}
}

here you use the fact that StreamReader implements
IDisposable and you don't need to implement it.

public class Bar
{
private StreamReader sr;
public Bar(string fnm)
{
sr = new StreamReader(fnm))
}
public M1()
{
// do something with sr
}
public M2()
{
// do something with sr
}
}

here you can not use the fact that StreamReader implements
IDisposable.

And you should write it as:

public class Bar : IDisposable

(and let Dispose handle sr).

Arne


Peter Duniho

unread,
Feb 25, 2011, 8:53:02 PM2/25/11
to

Just to clarify: this includes both unmanaged resources kept explicitly
(e.g. through p/invoke-related data structures, .NET operations that can
affect unmanaged resources, etc.) and unmanaged resources kept
implicitly (i.e. by having ownership of any other object that implements
IDisposable).

In this context, "unmanaged resource" is pretty much anything except an
actual in-memory object.

Arne's follow-up post does show the "implicit" scenario, but IMHO it's
worth calling out here.

The "explicit" scenario is one of those "if you don't know what it
is/means, then you don't need it." :) By the time you are knee-deep in
explicitly using unmanaged resources in your managed code, you are
likely to already have a firm understanding of IDisposable, when and how
to use it. Most people never ever find a need for that aspect of it.

MSDN has a number of articles discussing exactly when and how to
implement IDisposable. As far as implementation goes, in general:

• If a type explicitly owns an unmanaged resource, one should
implement a finalizer (if not already implemented in a base class), a
protected Dispose(bool) method, and a Dispose() method (for IDisposable)
that calls Dispose(bool) with the value of 'true'.

The Dispose(bool) method will examine the bool argument and only dispose
IDisposable objects if set to 'true'. It will be set to 'false' when
called from the finalizer, and in that case those IDisposable objects
may well have already been finalized, and in any case will be, so
there's no need to dispose of them at that time.

• If a type only implicitly owns an unmanaged resource (i.e. because
it owns some IDisposable object), then the finalizer and Dispose(bool)
method may be omitted. In that case, the Dispose() method would just
dispose the IDisposable object(s) owned by the object.

On the latter point: note that unless a class is sealed, it may be
inherited by a class that _does_ need to override the IDisposable
behavior, including adding a finalizer and Dispose(bool) method. So
unless you can seal the class, it's usually a good idea to just go ahead
and implement the full IDisposable pattern, even if it's not needed at
the time.

See http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx and
http://msdn.microsoft.com/en-us/library/b1yfkh5e.aspx for more details.

Pete

0 new messages