2012-11-16

Properly using and implementing the IDisposable pattern in .NET

IDisposable is a pattern in .NET that is used to implement deterministic cleanup of unmanaged resources. .NET has a garbage collector, but that is only used to manage memory, not resources like I/O (file handles, sockets, …). While in C++ destructors are called deterministically when an object goes out of scope, finalizers in C# get called by the garbage collector, but we can’t determine when that will happen (indeterministic deallocation). Don't depend on the garbage collector for unmanaged resources!

Properly disposing resources

The IDisposable-interface has a single method void Dispose(). This method is used to clean up the disposable object and release the unmanaged resources it holds. To make sure that you always call it, even if an exception occurs in the code between creating the object and calling its Dispose-method, you should use the try-finally-construct, like this:

var x = new X(); // X is a class that implements IDisposable
try
{
    // do something with x
}
finally
{
    x.Dispose();
}

Because this pattern is so common, C# contains the using-construct that does this more succinctly:

using (var x = new X())
{
    // do something with x
}

The advantage of this construct is that you can easily nest it without writing a lot of code:

using (var stream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
    using (var writer = new StreamWriter(stream, Encoding.UTF8))
    {
        foreach (string line in lines)
        {
            writer.WriteLine(line);
        }
    }
}

Or with less indentation and curly braces:

using (var stream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
using (var writer = new StreamWriter(stream, Encoding.UTF8))
{
    foreach (string line in lines)
    {
        writer.WriteLine(line);
    }
}

This is (more or less) equivalent to the following code:

var stream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
try
{
    var writer = new StreamWriter(stream, Encoding.UTF8);
    try
    {
        foreach (string line in lines)
        {
            writer.WriteLine(line);
        }
    }
    finally
    {
        writer.Dispose();
    }
}
finally
{
    stream.Dispose();
}

Properly implementing IDisposable in your own classes

Here’s the minimal proper implementation of IDisposable for a regular class that can be inherited (i.e. is not sealed).

public class MyDisposableType : IDisposable
{
    ~MyDisposableType()
    {
        // the finalizer also has to release unmanaged resources,
        // in case the developer forgot to dispose the object.
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);

        // this tells the garbage collector not to execute the finalizer
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            // clean up managed resources:
            // dispose child objects that implement IDisposable
        }

        // clean up unmanaged resources
    }
}

In the classes that derive from such a disposable class, you don't have to implement the entire pattern again: you can just override the Dispose(bool disposing)-method to add extra clean-up code. Just make sure you call the base-method (after the added code, in a finally-block).

public class MyDerivedDisposableType : MyDisposableType
{
    protected override void Dispose(bool disposing)
    {
        try
        {
            if (disposing)
            {
                // clean up managed resources:
                // dispose child objects that implement IDisposable
            }

            // clean up unmanaged resources
        }
        finally
        {
            // always call the base-method!
            base.Dispose(disposing);
        }
    }
}

If your class is sealed (so there can be no derived classes), you cannot have virtual methods. To properly implement the IDisposable-pattern, you could make the Dispose(bool disposing)-method private and not virtual:

public sealed class MySealedDisposableType : IDisposable
{
    ~MySealedDisposableType()
    {
        // the finalizer also has to release unmanaged resources,
        // in case the developer forgot to dispose the object.
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);

        // this tells the garbage collector not to execute the finalizer
        GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        if (disposing)
        {
            // clean up managed resources:
            // dispose child objects that implement IDisposable
        }

        // clean up unmanaged resources
    }
}

2012-03-22

GitHub & Gist

I decided to create a GitHub-account, specifically to manage the code snippets I occasionally publish on this blog via Gist. Now you get nice syntax-coloring, and it’s easier for me to manage and update the snippets.

My GitHub account-name is tommy-carlier, and you can find my code snippets here.

2011-11-17

Removing diacritics from a string

Recently I had to write a function that removes all diacritics from a string (e.g.: turning José into Jose). Searching the web, I quickly found the blog post “Stripping is an interesting job” by Michael Kaplan. His code is simple and good, but I saw some opportunities for optimizations (obvious stuff): because we know the approximate length (actually the maximum length) of the resulting string, we could give the StringBuilder-instance an initial capacity equal to the length of the original string. In some simple cases I actually like using a char-array instead of a StringBuilder, because it has even less overhead. Another obvious optimization is to check whether the original string is not empty. Here's my optimized version:

2010-03-04

Google Analytics in ASP.NET

Here's a little C# function I occasionally use to insert a Google Analytics-snippet in an ASP.NET website. It takes a Google Analytics-ID as an argument, which you can leave blank in debug-mode (to not render the snippet). It uses the recently introduced asynchronous loading method (which doesn't block the rendering of your website). It's not literally the same snippet you can find on the Google Analytics site, but it does the same thing (only with a slightly smaller dynamically built JavaScript-snippet).

I usually keep the Google Analytics-ID in my application settings (web.config), which allows me to use a different one for different deployments (or an empty one for debugging). So I have a second function (in a SiteUtils-class) that doesn't take an argument, but takes the Google Analytics-ID from the settings:
public static string RenderGoogleAnalyticsScript()
{
  return TC.GoogleAnalytics.RenderScript(Settings.Default.GoogleAnalyticsID);
}
In my master-page, I then use the following snippet to insert it into the body of the page (note that you have to use the fully qualified type name, including namespace):
<%= MyNamespace.SiteUtils.RenderGoogleAnalyticsScript() %>
Updated 2010-06-03: slightly modified the generated JavaScript-code to better match the current official Google Analytics-script.
Updated 2012-03-22: made the generated JavaScript-code even smaller, and moved the code snippet to Gist.

2010-01-08

if-read-do-while-read part 2

I just received a comment on my previous post about the if-read-do-while-read construction. Tanton suggests that I could create a method that encapsulates this construction, using delegates for the before, during and after steps. Here's a quick version of an extension method on IDataReader that does this:

public static class DataReaderExtensions
{
  public static void ReadAll(
    this IDataReader reader,
    Action before,
    Action<IDataRecord> during,
    Action after)
  {
    if (reader == null) throw new ArgumentNullException("reader");

    if (reader.Read())
    {
      if (before != null)
        before();
      
      do
      {
        if (during != null)
          during(reader);
      }
      while (reader.Read());
      
      if (after != null)
        after();
    }
  }
}

And here's a rewrite of the HTML-example, using this extension method:

StringBuilder html = new StringBuilder();
using (IDataReader reader = command.ExecuteReader())
  reader.ReadAll(
    () => html.Append("<ul>"),
    record => html.AppendFormat("<li>{0}", record.GetValue(0)),
    () => html.Append("</ul>"));

It surely is shorter, but I'm not entirely convinced it's cleaner.

2009-12-29

if-read-do-while-read

Here’s a construction I occasionally need when reading items from a IDataReader: to execute some code before and after reading through the IDataReader, but only if there are rows in the IDataReader.

One option is to use a boolean variable that determines whether the collection has items, like this:

using (IDataReader reader = command.ExecuteReader())
{
  bool hasRows = false;
  while (reader.Read())
  {
    if (!hasRows)
    {
      hasRows = true;
      Before();
    }

    During();
  }

  if (hasRows)
    After();
}

But the following construction (which I call the if-read-do-while-read construction) does not need the boolean variable and is cleaner:

using (IDataReader reader = command.ExecuteReader())
{
  if (reader.Read())
  {
    Before();

    do
    {
      During();
    }
    while (reader.Read());

    After();
  }
}

Here's a practical example: creating HTML-code with a UL-list for the data, but only if there are items:

StringBuilder html = new StringBuilder();
using (IDataReader reader = command.ExecuteReader())
{
  if (reader.Read())
  {
    html.Append("<ul>");

    do
    {
      html.AppendFormat(
        "<li>{0}",
        reader.GetValue(0));
    }
    while (reader.Read());

    html.Append("</ul>");
  }
}

You could use the same technique on an IEnumerable<T>, but you wouldn't be able to use foreach anymore:

using (var enumerator = collection.GetEnumerator())
{
  if (enumerator.MoveNext())
  {
    Before();

    do
    {
      var item = enumerator.Current;
      During();
    }
    while (enumerator.MoveNext());

    After();
  }
}

2009-11-12

NDepend

A while ago, I received an e-mail from Patrick Smacchia, offering me a license for NDepend and asking me whether I could try it out, give some feedback and if I liked it, maybe I could blog about my experience with NDepend. Because I was already subscribed to the RSS-feed of Patrick’s blog, I knew about NDepend and how it could help developers write better code. I like trying out stuff like this, but somehow I hadn’t found time to install and properly test it. I was already using Visual Studio’s Code Analysis (FxCop) and StyleCop to analyze my code and improve its quality, so I never had the right incentives to use additional tools. But it would be rude to just say no to Patrick, so I decided to give it a try.

After installing it (which just means downloading: NDepend is a portable application that doesn’t really have to be “installed”), I started Visual NDepend (the GUI version of NDepend) and was greeted with a start page that looks like the Visual Studio start page. I decided not to just jump in and try stuff, but to first watch some of the screen casts and tutorials on the NDepend website. And I’m glad I did. After trying to analyze some of my projects, I discovered that NDepend produces a lot of data, reports and graphs that only make sense after you have learnt what it all means. This is a tool that you have to learn how to use and how to interpret its results. The user interface is very flexible, but also a bit overwhelming. But because I don’t really have an idea how I would design a UI for an application like this, I can’t really criticize it.

Comparing it to FxCop and StyleCop is not really fair. There is some overlapping functionality between the 3 tools, but NDepend offers a lot more. First of all, FxCop and StyleCop just analyze your code according to fixed rules and the result is a bunch of errors and warnings. NDepend can also be used like this, but the rules are not fixed and are defined in CQL, a query language that looks and feels like SQL, but allows you to query code constructs. The advantage of this approach is that you can write ad-hoc queries to gain insight in your code (or someone else’s assemblies). Here’s an example of a CQL query:

WARN IF Count > 0 IN SELECT TOP 10 TYPES WHERE NbMethods > 20 ORDER BY NbMethods DESC

This query will generate a warning if your code has classes (and structs) with more than 20 methods. All the built-in rules are written as CQL queries, which you can just modify (or you could just create additional rules).

Another area of analysis that NDepend offers that FxCop and StyleCop do not have is the visual views of your projects. There’s a metrics treemap that shows the proportions of different metrics of your project (methods, types, fields, …) and there are 2 ways to show dependencies: a dependency graph and a dependency matrix. While the dependency graph looks familiar and very graphical, it fails when there are too many elements and dependencies. This is not a failure of NDepend, but just a limitation of dependency graphs in general. Dependency matrices on the other hand are great to understand dependencies, even when there are a lot of elements, but they are less intuitive and you have to learn how to use them and interpret what you see.

My conclusion is that NDepend is a great tool to analyze your code and improve its quality, but it takes getting used to. You have to learn how to use it and how to interpret the results you get. I feel like I have just seen the tip of the iceberg. NDepend also contains a console-application, and can be automated and integrated in your build process (MSBuild, NAnt, …). It has add-ins for Visual Studio and Reflector, and can be used to compare different versions of a code base (for example: to see if compatibility is broken).

If you want to try it yourself, I suggest you first watch the screen casts, download NDepend and start analyzing some of your projects. Another suggestion I have is to start reading Patrick Smacchia’s blog, because he has written about some cool things you can do with NDepend. He also explains how he has analyzed some existing .NET applications (Paint.NET, NHibernate, SharpDevelop, …) and even how he uses NDepend to find what has changed between different versions of the .NET Framework BCL.