Jump to content
  • Advertisement
Sign in to follow this  
TheBluMage

[.net] Close = Dispose. Can I do something like that?

This topic is 4851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I was just curious about why classes like Stream can implement IDisposable and rename the Dispose method name to Close and how one could do this with their own classes.

Share this post


Link to post
Share on other sites
Advertisement
They do not rename Dispose to Close, they call Dispose(true) in close.

Share this post


Link to post
Share on other sites
Yeah, but there's no publicly accessible Dispose method in Stream, which implements IDisposable. Attempting to call Dispose on a Stream or any of its derived classes results in a compile-time error along the lines of:

'System.IO.Stream' does not contain a definition for 'Dispose'

or

'System.IO.FileStream.Dispose(bool)' is inaccessible due to its protection level

Can a class implement an interface without having to publicly expose its members? I know that you can cast the Stream to an IDisposable object and call Dispose on that, but it seems like the Stream class hid the method in favor of the publicly accessible and more appropriately named Close method.

Share this post


Link to post
Share on other sites
Yes, it's called implicit implementation

public class Blah : IDisposable {
void IDisposable.Dispose() {
}
}

To access it you would need to cast it to an IDisposable:

Blah b = new Blah();
IDisposable d = (IDisposable)b;
d.Dispose();


However, the typical implementation for a stream goes like this:

public class Thingy : IDisposable {
protected Dispose(bool disposing) {
if(!disposed) {
if(disposing) {
//Call dispose on managed resources
}
//Dispose of unmanaged resources
disposed = true;
}
}

void IDisposable.Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}

~Thingy() {
Dispose(false);
}

private bool disposed = false;
}

Share this post


Link to post
Share on other sites
When you program in VB.NET you can do it by coding like this:


Public Sub Close() Implements IDisposable.Dispose
'...
End Sub





I don't like it but you can use it.

BTW: Close and Dispose are not always the same thing! Close on a database connection will make the connection available to the connectionpool whereas Dispose will not!

Cheers

Share this post


Link to post
Share on other sites
Yeah, I get it now - I just never realized that the methods from the interface didn't have to be declared public. Thanks for all your help!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!