Click here to Skip to main content
15,923,120 members
Articles / Programming Languages / C#

C# IDisposable Pattern on Sub-classes

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
21 Jul 2011CPOL 19.2K   2   2
C# IDisposable pattern on sub-classes

Today I was faced with the following situation:

I had a base-class which was implementing the IDisposable pattern (including the GC.SuppressFinalize() part). From this class, I inherited a sub-class which also required doing some disposing stuff.

Now I asked myself how I should implement the IDisposable pattern correctly, that the dispose methods were not called twice (and throwing an ObjectDisposedException) and all my managed objects were disposed properly...

After some Googling around, I found an interesting blog-article:

There it is clearly explained that you only have to override the Dispose(bool disposing) method (and that you do not require to implement the IDisposable interface)!

Small Example

You have the following base class:

C#
/// <summary>
/// <see cref="IDisposable" /> interface example.
/// </summary>
public class Example : IDisposable
{
	/// <summary>
	/// Flag stating if the current instance is already disposed.
	/// </summary>
	private bool _disposed;

	/// <summary>
	/// The <see cref="IDisposable" /> implementation.
	/// </summary>
	public void Dispose()
	{
		this.Dispose(true);
		GC.SuppressFinalize((object)this);
	}

	/// <summary>
	/// Dispose method, releasing all managed resources.
	/// </summary>
	/// <param name="disposing">States if the resources should be disposed.</param>
	protected virtual void Dispose(bool disposing)
	{
		if (_disposed)
		{
			return;
		}

		if (disposing)
		{
			// Dispose all managed resources here.
		}

		_disposed = true;
	}
}

If you now want to inherit a class from the above base-class, you have to implement the sub-class in the following way:

C#
/// <summary>
/// SubClass of a <see cref="IDisposable" /> implementing base-class.
/// </summary>
public class SubExample : Example
{
	/// <summary>
	/// Flag stating if the current instance is already disposed.
	/// </summary>
	private bool _disposed;

	/// <summary>
	/// Dispose method override, releasing all managed resources of the sub-class.
	/// </summary>
	/// <param name="disposing">States 
	/// if the resources should be disposed.</param>
	protected override void Dispose(bool disposing)
	{
		if (_disposed)
		{
			return;
		}

		if (disposing)
		{
			// Dispose all managed resources here.
		}

		_disposed = true;

		base.Dispose(disposing);
	}
}

That's it! :)

If you have any suggestions or questions, feel free to write a comment! :)

Happy coding!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Switzerland Switzerland
I'm a software developer with ASP.NET experience since 2007

Projects:
=========
See my projects list here: http://dotnetcorner.ch/Home/Projects

=========
More about me: http://dotnetcorner.ch
My Blog: http://dotnetcorner.ch/Home/Blog

Comments and Discussions

 
QuestionMessage Closed Pin
28-Oct-20 6:55
Member 1495839728-Oct-20 6:55 
QuestionWhat if you can't change the Dispose() method on the base class to virtual? Pin
albsilva_m2u9-Sep-11 0:38
albsilva_m2u9-Sep-11 0:38 
AnswerRe: What if you can't change the Dispose() method on the base class to virtual? Pin
Christoph Keller12-Sep-11 19:09
Christoph Keller12-Sep-11 19:09 
Hello,

Sorry for the late response, I was in vacations this days Cool | :cool:

In the case of a third-party / system class, I would try to implement the IDisposable this way:

C#
/// <summary>
/// Example of a <see cref="IDisposable"/> implemented in a third-party class.
/// </summary>
public class DisposableTest : UserControl
{
	/// <summary>
	/// Flag stating if the current instance is allready disposed.
	/// </summary>
	private bool _disposed;

	/// <summary>
	/// The override of the base-dispose function.
	/// </summary>
	public override void Dispose()
	{
		this.Dispose(true);
		base.Dispose();
		GC.SuppressFinalize((object)this);
	}

	/// <summary>
	/// Dispose method, releasing all managed resources.
	/// </summary>
	/// <param name="disposing">States if the resources should be disposed.</param>
	protected virtual void Dispose(bool disposing)
	{
		if (_disposed)
		{
			return;
		}

		if (disposing)
		{
			// Dispose all managed resources here.
		}

		_disposed = true;
	}
}


If the IDisposable interface is not already implemented in the base class, I would implement the interface in the default way (as shown in the first example of the article).

Any objection of the above example? Please let me know, I'm not a IDisposable expert and would like to learn something Wink | ;)

Have a nice day and always happy coding,
Chris
New Url Un-Shortener available: http://dotnetcorner.ch/tools/unshortener/

Check out my website http://dotnetcorner.ch/ and my blog http://blog.dotnetcorner.ch/

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.