Object Dumper: An Invaluable Tool for Writing Code in the Functional Programming Style

When developing C# programs in the functional programming style, you often need to dump out a collection to the console.  Object dumper is a great tool to use for this.  It is a sample that far too few developers know about.

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOC
A functional transform typically takes the form of successive transformations: transform collection a => collection b => collection c => final collection.  This is one of the main points of this topic in my Functional Programming Tutorial.

When I commence developing such a transform, I first develop a model in my mind of these transforms.  Sometimes (particularly when writing ad-hoc transformations) I don’t fully design each transform before starting coding.  For instance, when the source collection is particularly messy, I first write a transform to simply clean up the data and transform it into a more usable form for subsequent transformations.  Sometimes I do a transform to an anonymous type, and sometimes I know straight away that I’m going to want to transform into a named type.  Regardless of which approach I take, I validate that my transform is as I want it before coding up the next transform.  The object dumper is my preferred tool for this.

One note: I typically don’t use the debugger very much when writing these transformations.  I write a transform, validate the results, write the next transform, validate again, and so on.  Due the lazy evaluation, using the debugger is necessarily not always convenient.  So I don’t worry about it, and just use Object Dumper.

To use it, just add ObjectDumper.cs to your project.

For the simplest use, you can simply pass any object to ObjectDumper:

var z = new
        A = "1",
        B = "2"

This produces the following output:

A=1     B=2

Sometimes you have nested collections:

var z = new
    Aaa = "Hello",
    Bbb = "There",
    Ccc = new[] { 1, 2, 3 }

This outputs:

Aaa=Hello       Bbb=There       Ccc=...

In this case, it would be useful to see the nested collection, so you can pass an additional argument to ObjectDumper that tells it to dump the first level of the nested collection:

var z = new
    Aaa = "Hello",
    Bbb = "There",
    Ccc = new[] { 1, 2, 3 }
ObjectDumper.Write(z, 1);

This produces the following:

Aaa=Hello       Bbb=There       Ccc=...
  Ccc: 1
  Ccc: 2
  Ccc: 3

You can find Object Dumper in the CSharpSamples.zip file that is installed with Visual Studio 2008.

C:Program FilesMicrosoft Visual Studio 9.0Samples1033CSharpSamples.zip

Because I do a lot with SharePoint and Hyper-V, I run the 64 bit version of Windows Server 2008 on my development machine, so in my case, CSharpSamples.zip is at:

C:Program Files (x86)Microsoft Visual Studio 9.0Samples1033CSharpSamples.zip

Comments (4)
  1. You only have to make sure to not use it when you’ve got something with an indexed property, because it fails miserably when confronted with those.

  2. Hi Omer,

    There’s a very good reason that ObjectDumper fails for indexed properties.  Indexed properties are basically just another syntax for an instance method that takes a single argument.  Trying to come up with the range of values that you could pass as arguments to a function is not one of the goals of ObjectDumper.


  3. Matt says:

    Hi Eric,

    This is cool.

    Sorry for simple question, but how can I get objectdumper to write to the IDE output window and not a console?


  4. John says:


    You can add this method and use it as you would the Write method.

       public static void WriteToOutputWindow(object element, int depth)


           TextWriter textWriter = new StringWriter();

           Write(element, depth, textWriter);





Comments are closed.

Skip to main content