Stephen Cleary "dot com"

I am Stephen Cleary, a Christian, husband, father, and programmer living in Northern Michigan.

This site is a collection of my projects and articles; I hope you find them useful!

Blog Open-source libraries and projects Online web applications Publications Online accounts Email
Home

Online accounts

Stack Overflow MSDN Forums CodePlex Twitter
Home

Online applications

  • Yet Another C# Code Formatter

    Formats C# code as HTML. My formatter accepts a (limited) set of commands, as well.

  • Guid Decoder

    Breaks down any GUID/UUID into its components.

Home

C# code formatter

Format C# Code

Advanced

The C# code formatter has two "extended commands":

  • Type names may be surrounded by backtick-dollarsign ("`$"), e.g., "`$Console`$.Write".
  • Highlighting may be turned on and off by backtick-exclamation-point ("`!"), e.g., "only `!some of this`! is important".

Preview in light theme (CSharp-Light.css):

Preview in dark theme (CSharp-Dark.css):

Home

GUID Decoder

Decode GUID

You can create your own GUID by running uuidgen or uuidgen -x, or use one of the examples below:

The GUID used to track down the author of the Melissa virus The ETW provider for ASP.NET events The Windows device class for floppy disk drives The GPT system partition on newer UEFI computers
Home

Open-source libraries and projects

  • Connected Properties

    Dynamically add properties to (almost) any object instance.

  • AsyncEx

    Helper types for developing with async and await.

  • Comparers

    The last comparison library you'll ever need!

  • ArraySegments

    A library for slicing and dicing arrays (without copying).

Home

ArraySegments

The ArraySegments library allows you to easily slice and dice ArraySegment<T> instances.

// Use the ArraySegments library.
using ArraySegments;

class Test
{
  public static void Playground()
  {
    byte[] myData = ...;
    ArraySegment<byte> data = myData.AsArraySegment();

    // Take, Skip, TakeLast, SkipLast, and Slice are all available.
    ArraySegment<byte> header = data.Take(1024); // Look ma, no copying!

    // BinaryReader/BinaryWriter/MemoryStreams also provided for convenience.
    using (var binaryReader = header.CreateBinaryReader())
    {
      int littleEndianInt32 = binaryReader.ReadInt32();
      ...;
    }
  }
}
Project page (CodePlex) NuGet package Documentation (CodePlex) Forum (CodePlex) Issue tracker (CodePlex) Browse source code (CodePlex)
Home

AsyncEx

This is a "fill in the gaps" sort of library for working with async and await. This library contains a large number of small utility classes.

One type is AsyncLazy<T>, which provides asynchronous lazy initialization. It executes the asynchronous initialization when it is awaited, and ensures that the initialization is only performed once.

// Use the AsyncEx library.
using Nito.AsyncEx;

class Test
{
  private static readonly AsyncLazy<MyResource> resource =
      new AsyncLazy(async () =>
      {
        var ret = new MyResource();
        await ret.InitializeAsync();
        return ret;
      });

  public static async Task MethodWhichUsesResourceAsync()
  {
    MyResource myResource = await resource;
    ...;
  }
}

Another useful type is AsyncLock, an asynchronous lock. Asynchronous methods may use the lock for synchronization without blocking (that is, the method is blocked, but the thread is not):

// Use the AsyncEx library.
using Nito.AsyncEx;

class Test
{
  private readonly AsyncLock sync = new AsyncLock();

  public async Task AccessSharedStateAsync()
  {
    using (await sync.LockAsync())
    {
      ...;
    }
  }
}
Project page (CodePlex) NuGet package Documentation (CodePlex) Forum (CodePlex) Issue tracker (CodePlex) Browse source code (CodePlex)
Home

Comparers

The Comparers library provides a semi-fluent API for defining how objects can be compared. This makes even the most complex comparisons relatively easy.

// Use the Comparers library.
using Comparers;

class Person
{
  public string FirstName { get; }
  public string LastName { get; }
}

class Test
{
  public static void SortByLastNameThenFirstName(List<Person> list)
  {
    IComparer<Person> nameComparer = Compare<Person>.OrderBy(p => p.LastName).ThenBy(p => p.FirstName);
    list.Sort(nameComparer);
  }
}

The Comparers library also provides assistance for defining the default comparison for a type:

// Use the Comparers library.
using Comparers;

class Person : ComparableBase<Person>
{
  public string FirstName { get; }
  public string LastName { get; }

  static Person()
  {
    // ComparableBase uses DefaultComparer to correctly implement:
    //   IComparable<Person>, IEquatable<Person>, IComparable, Object.Equals, Object.GetHashCode.
    DefaultComparer = Compare<Person>.OrderBy(p => p.LastName).ThenBy(p => p.FirstName);
  }
}
Project page (CodePlex) NuGet package Documentation (CodePlex) Forum (CodePlex) Issue tracker (CodePlex) Browse source code (CodePlex)
Home

Connected Properties

Connected Properties allow you to dynamically attach properties to any reference type that uses reference equality.

The connected properties have lifetimes exactly like regular properties (even if they reference their carrier object). A connected property is a true ephemeron.

// Use the Connected Properties library.
using Nito.ConnectedProperties;
using Nito.ConnectedProperties.Named;

class Program
{
  // Display the Name of any object passed into this method.
  public static void DisplayName(object obj)
  {
    // Look up a connected property called "Name".
    var nameProperty = obj.GetConnectedProperty("Name");
    Console.WriteLine("Name: " + nameProperty.Get());
  }

  static void Main()
  {
    // Create an object to name.
    var obj = new object();

    // I dub this object "Bob".
    var nameProperty = obj.GetConnectedProperty("Name");
    nameProperty.Set("Bob");

    // Pass the object to the DisplayName method, which is able to retrieve the connected property.
    DisplayName(obj);
  }
}

Note: You should not develop a design using Connected Properties. The Connected Properties library should only be used to work around limitations in third-party code.

Project page (CodePlex) NuGet package Documentation (CodePlex) Forum (CodePlex) Issue tracker (CodePlex) Browse source code (CodePlex)
Home

Publications

  • Trade journals
  • Best Practices in Asynchronous Programming

    A "second step" for learning async, providing guidelines to avoid common pitfalls. Includes several cheat sheets and resources for async development.

    MSDN Magazine, March 2013

  • It's All About the SynchronizationContext

    A thorough look at the SynchronizationContext type, starting with some history and concluding with how it is used by various components (including components in development such as the async / await compiler support).

    MSDN Magazine, February 2011

  • C++ Type Traits

    A brief description of the Boost.TypeTraits library, which uses advanced templates to determine (and manipulate) type properties at compile-time. This was the first article on template metaprogramming; the Type Traits library provided the groundwork for more advanced template metaprogramming. I co-authored this article with John Maddock.

    Dr. Dobb's Journal, October 2000

  • Online
  • Async and Await

    An introduction to async and await.

    Blog

  • TCP/IP .NET Sockets FAQ

    How to use TCP/IP sockets from .NET (correctly). I've gotten a lot of thankful responses on the Message framing and Detection of half-open connections sections in particular.

    Blog

  • Implementing IDisposable and Finalizers

    Three simple rules for IDisposable and finalizers. These rules are derived from my background in reliable programming, and are far simpler than the "dispose pattern" officially recommended by Microsoft.

    Blog

  • IDisposable: What Your Mother Never Told You about Resource Deallocation

    An early article discussing some of the problems around nondeterministic resource deallocation and IDisposable. I was still quite new to .NET at the time.

    Code Project