A way to pretty print a C# object

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


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

}

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.