2013-09-30

Great explanation of ASCII, Unicode and UTF-8

In the following video (9:37), Tom Scott gives an overview of the history of ASCII, Unicode and UTF-8, providing a great explanation how it works. youtu.be/MijmeoH9LT4

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:

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

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

Or with less indentation and curly braces:

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

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):

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):

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:

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();
  }
}