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?
-
1What do you mean by display a C# object? Display its full name, its properties or what? – Erick Petrucelli Jun 10 '11 at 16:23
10 Answers
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:

- 21,728
- 13
- 62
- 101
-
Great answer and the example works but calling the object to be serialized `property` is kinda misleading. Or maybe I'm missing something? – z33k Oct 14 '20 at 11:20
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; }
}

- 796
- 6
- 13
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.

- 10,043
- 2
- 40
- 59
-
3I actually like this idea better than a roll-your-own ToString(). a JSON serialization will show every public field and its value in a much "lighter" format than XML, without touching ToString which may be used elsewhere. Here's a simple ToJSON extension method: http://weblogs.asp.net/scottgu/archive/2007/10/01/tip-trick-building-a-tojson-extension-method-using-net-3-5.aspx – KeithS Jun 10 '11 at 16:30
-
a quick and dirty example would be useful here, especially since this might be needed the most by newcomers of the language! – Redoman Mar 04 '16 at 06:59
-
9Newtonsoft's `JsonConvert` can has formatting options as well `Debug.WriteLine(JsonConvert.SerializeObject(myObject, Formatting.Indented))` – jreancsu Mar 12 '18 at 19:06
-
I use pretty printed objects in unit test assertions. It is a quick way ( cut'n'paste ) to assert that all fields are present and have a certain value. Huge speedup in unit test production. – Fred Haslam Jun 11 '21 at 05:55
This worked very sweet for me:
string SerilizedText = "";
RootClass myclass= new Root RootClass ();
SerilizedText = JsonConvert.SerializeObject(myclass,Newtonsoft.Json.Formatting.Indented) ;

- 7,003
- 4
- 40
- 40

- 71
- 1
- 3
An easy and simple way is to override the ToString
method.
Here's a link: How to override ToString

- 1,219
- 1
- 20
- 37

- 8,319
- 3
- 38
- 52
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 :-)

- 2,513
- 2
- 26
- 36
Simply override ToString()
on your type and provide your own, formatted string for debug display.

- 122,712
- 22
- 185
- 265
- Install-Package ServiceStack.Text
obj.Dump();
If you don't want to use external libs, write your own Dump() extension method.

- 1,844
- 1
- 19
- 26
-
obj.ToDump() lets VS find the right package to help with your using include – BozoJoe Jun 24 '16 at 00:14
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);
}

- 150
- 3
-
Avoid code only solutions. Explain with a link/description as the OP could be an inexperienced programmer – Matthew Swallow Aug 31 '22 at 12:45