A way to pretty print a C# object

C#Pretty Print

C# Problem Overview


I have a text box and I want to display a C# object in it in a human-readable way, just for debugging reasons. I don't want to use external libraries if possible. How do I do this?

C# Solutions


Solution 1 - C#

If you use Json then I would suggest using Newtonsofts Json library and then you can output the entire object in Json notation and it will format it with spacing and line breaks. we have used this to display complex objects easily for debug purposes:

var jsonString = JsonConvert.SerializeObject(
           complexObject, Formatting.Indented,
           new JsonConverter[] {new StringEnumConverter()});

here I have also used the String Enum converter in order to display Enums as their string representation rather than as an integer.

The library is available through NuGet as Json.Net or Newtonsoft Json

Or you can get it here:

https://www.newtonsoft.com/json

Solution 2 - C#

If it is just for debugging purposes, use the DebuggerDisplayAttribute.

Using this attribute will change what the object looks like in the Value section of the watch window (or ont he mouse-over during debugging)

usage:

[DebuggerDisplay("Name = {FirstName} {LastName}")]
public class Person {
  public string FirstName { get; set; }
  public string LastName { get; set; }

}

Solution 3 - C#

Serialize it to JSON. It can be done in the ToString() method like others suggested, but I don't think that is appropriate if you are going to use that for debugging only.

Solution 4 - C#

An easy and simple way is to override the ToString method.

Here's a link: How to override ToString

Solution 5 - C#

This worked very sweet for me:

string SerilizedText = "";
RootClass myclass= new Root RootClass ();
SerilizedText = JsonConvert.SerializeObject(myclass,Newtonsoft.Json.Formatting.Indented) ;

Solution 6 - C#

i also used Json for a while; but now I created a PrettyPrint-Minimod. You can add it via Nuget (it is a Sourcecode distribution). Find out what a Minimod is here.

It nicely prints object graphs and has some magic for enumerables and dictionaries. It also tries to figure out proper line-breaks.

I'll be blogging about it soon - but just go ahead and try it :-)

Solution 7 - C#

Simply override ToString() on your type and provide your own, formatted string for debug display.

Solution 8 - C#

I use this quite a bit to populate list boxes with custom objects:

public override string ToString()
{
    return String.Format("{0}:{1}:{2}", Property1, Property2, Property3);
}

Solution 9 - C#

  1. Install-Package ServiceStack.Text
  2. obj.Dump();

If you don't want to use external libs, write your own Dump() extension method.

Solution 10 - C#

Override the .ToString() method.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHuiView Question on Stackoverflow
Solution 1 - C#RichardView Answer on Stackoverflow
Solution 2 - C#vt100View Answer on Stackoverflow
Solution 3 - C#Ilia GView Answer on Stackoverflow
Solution 4 - C#Gustavo MoriView Answer on Stackoverflow
Solution 5 - C#mariaView Answer on Stackoverflow
Solution 6 - C#Lars CorneliussenView Answer on Stackoverflow
Solution 7 - C#Ed S.View Answer on Stackoverflow
Solution 8 - C#JonView Answer on Stackoverflow
Solution 9 - C#LessneekView Answer on Stackoverflow
Solution 10 - C#JayView Answer on Stackoverflow