Do you have an object and want to display all of its values at runtime in C#, without having to open specific debugging tools? In this article, I am going to explain the ways to be able to easily print out or display the values of an object along with all its nested objects.
Dumping objects is a great way to visualize values and types while enabling you to easily debug and detect problems at runtime. This is particularly important whenever you don’t have access to specific debugging tools.
If you are a PHP developer or at least know some PHP, you might already be familiar with a very simple commonly used function that prints out (or dumps) the full details of variables (or objects), including the value, the datatype and the length for
string types, this function is the
I don’t want to dig deep into PHP now, as it is outside the scope of this article, however I just want to quickly mention that due to the nature of PHP being a dynamic language, the dumping is done easily by its built-in function
In C#, you can still achieve the same result as PHP, but unluckily there is no built-in functionality to do so as PHP. So in C#, you must either use reflection or serialization to be able to dump the variable or object that you have.
Luckily though, there are many ways to do this in C#, and I am going to explain these methods to you in this article.
While there might be some other methods that I am unaware of to achieve a similar result, I will be explaining 3 ways, just to keep it short for you. I will be so happy if you share more ways that you know so all of us can learn from each other.
So as mentioned before, there are numerous ways to dump an object to display all of its details. Let’s get started.
1. Using Object Dumper
ObjectDumper is a very handy assembly that can be found under Nuget Packages.
Once installed, the assembly provides a simple single
static method (
Dump) that takes a Generic type
Dump name and a
stream writer as parameters.
The idea of the object dumper is that it recursively loops on the object’s properties through reflection and collects all data underlying that object.
Check the below code snippet to implement the
Item item = new Item
Name = "Chocolate",
Number = 12345,
CreatedDate = DateTime.Now,
Price = 36.7M,
Category = new Category(1, "Sweets")
using (var writer = new System.IO.StringWriter())
ObjectDumper.Dumper.Dump(item, "Object Dumper", writer);
The writer object will display the following output on the console:
2. Using JSON Serializer
Serializing an object means to transform or convert this object (of some type), to a
string representation using a formatter. Now there are many formatters in C# that can be used to serialize objects.
The first and most commonly used types (nowadays) is the json formatter. ( You can still use other formatters like XML formatter, but I think json will be a better option due to its simplicity and better readability.)
To be able to serialize an object using the json serializer, you need to import the
Newtonsoft nuget package, and then call the
SerializeObject from class
JsonConvert, passing to it the object you want to serialize as the first argument, and the second argument will be an
enum of how to format the json string output, like indentation.
See the below code snippet that represents a
Dump function, it takes the object and dumps it to the console output. You can dump your object wherever you like (log, file, etc.)
private static void Dump(object o)
string json = JsonConvert.SerializeObject(o, Formatting.Indented);
Now that you have the basic concept of serializing the object to a json
string and then dump it, why not we improve the above function to let it become an extension method? (Read my blog post about Extension Methods in .NET.
Having such function as an extension method on the project’s level comes in handy whenever you want to debug or visualize the details of your objects at runtime by just calling the
dump method through the object’s reference.
The below code is the same
dump function mentioned previously, with a twist of an extension method:
static class ObjectHelper
public static void Dump<T>(this T x)
string json = JsonConvert.SerializeObject(x, Formatting.Indented);
Then you can simply call the method
dump on our example’s
item object (just make sure to import the
namespace of the
ObjectHelper in case you defined it under a different
The output of the
dump method that uses the json serializer is shown as below:
3. Using YAML
YAML stands for Ain’t Markup Language, according to yaml.org:
YAML is a human-friendly data serialization standard for all programming languages.
And in our context, YAML can also serve our need pretty well, in .NET, you can install the
YamlDotNet nuget package, convert the object to a YAML format and then do the needed dumping.
The below function can be used to dump some object in YAML format:
private static void DumpAsYaml(object o)
var stringBuilder = new StringBuilder();
var serializer = new Serializer();
serializer.Serialize(new IndentedTextWriter(new StringWriter(stringBuilder)), o);
Calling the above
DumpAsYaml function on our item object will result in displaying the object’s details in YAML format, as shown in the below console output:
It is very useful to be able to visualize your objects or collections at runtime, without having the need to open a particular debugging tool, like the debugger of Visual Studio.
In this article, I explained 3 ways to be able to dump an object at runtime so you can visualize the object values.
Let me know if this article was clear enough to explain this topic, and if you know more ways to dump objects, feel free to share them in your comments.
A passionate software developer with 12+ years of overall experience in various development languages including C#/vb.net, java. The technologies I mostly focus on are: ASP.NET Core, Android, Angular
I currently work as a Corporate Technical Manager in the Digital Solutions Team at Aramex International in Amman, Jordan.