Geeks With Blogs

News This is the *old* blog. The new one is at blog.sixeyed.com
Elton Stoneman
This is the *old* blog. The new one is at blog.sixeyed.com

My latest Pluralsight course has been released! It’s called:

IDisposable Best Practices for C# Developers

It covers what you need to know about IDisposable, and why you need to know about it.

IDisposable is a simple interface and it’s simple to use. But if you read about IDisposable, most sources describe it in the context of unmanaged resources, so when you’re building a pure managed app, you could well think you don’t need to worry about IDisposable - but you’d be wrong. In the course I start with a simple demo using the SqlClient class in the .NET framework, working with instances and not disposing them, which quickly exhausts the connection pool and crashes the app.

Bad things happen when you work with databases, or files, or WCF services and don’t dispose objects correctly, and they’re just the obvious examples. A good understanding of IDisposable is critical to writing performant, reliable apps that don’t have any lurking, hard-to-find bugs. The course walks through eight Best Practices that I’ve built up after spending many years tracking down nasty problems, and finding misuse of disposable objects to be the cause.

Here’s what the Pluralsight peer  review for the course said:

This course should be promoted as a foundational course for C# developers because it clearly shows that memory management issues don't go away simply by adding a garbage collector... somebody has to put out trash.

The course is delivered over four modules:

Introducing IDisposable

  • Covers the IDisposable interface, implementing it in your classes and working with IDisposable instances
  • Shows how not using IDisposable correctly can cause crashes and functional defects
  • Looks at why IDisposable applies to managed resources as well as unmanaged
  • Walks through the demo solution, which ignores IDisposable and has lots of issues to fix, and shows Best Practice #1

What Happens When the Garbage Collector Runs?

  • Looks at what the .NET Garbage Collector does and how it works
  • How the GC needs to run quickly and infrequently but still keep memory available
  • Where Dispose and Finalize fit with memory management
  • Walks through the correct implementation and use of IDisposable, and Best Practices #2 – #5

What Happens if you Don’t Dispose?

  • Looks at common issues – file locks, repeating event handlers, starving the SQL connection pool and object leaks
  • Walks through how to find problems, at run-time with profiling and at design time with static analysis
  • Walks through fixing all the issues in the demo solution, and Best Practices #6 – #8

Course Summary

  • Looks at the cost of not using IDisposable correctly
  • Recaps all eight Best Practices, prioritising them using MOSCoW

This is mostly a “why” course, focusing on why you need to have a good understanding of IDisposable, and why you should apply the best practices in every solution you build.

Hope you find it useful.

Posted on Monday, June 30, 2014 6:47 PM Pluralsight , C# | Back to top


Comments on this post: IDisposable Best Practices for C# Developers

# re: IDisposable Best Practices for C# Developers
Requesting Gravatar...
I'm trying to understand something You said that the GC can't release unmanaged objects. I opened a file (from code) and saw that I can't open it because another process (my app is using it).
I closed the app while not disposing the filestream. After that I tried to open the file again and saw that I could do it (so it was released).
So what happen here?
Left by Avi on Dec 21, 2014 11:41 AM

Your comment:
 (will show your gravatar)


Copyright © Elton Stoneman | Powered by: GeeksWithBlogs.net