2

While I was watching a tutorial video about Csharp async and await concepts, I encountered the Dump() method for the first time. The instructor used this method as follows:

public string BoilWater()
{
"Start the kettle".Dump();
"Waiting for the kettle".Dump();
"Kettle finished boiling".Dump():
return water;
}

And here is the output for this code:

Start the kettle
Waiting for the kettle
Kettle finished boiling

Although I'm a newbie in Csharp, I already have some knowledge about the LINQ from reading the book Programming C# 5.0 and I worked a little bit with LINQPad. And I have done some search about it, but to be honest I couldn't find much about Dump() method. So, here is my question, could we just use the Console.WriteLine for creating this output? What is the advantage of using Dump() over Console.WriteLine here?

Roksana
  • 264
  • 2
  • 16
  • Can't decide if it should be duplicate of https://stackoverflow.com/questions/3555317/linqpad-extension-methods or explaining how Console.WriteLine uses Dump in LinqPad would be good answer... Leaving as comment since I don't actually know the exact difference. – Alexei Levenkov Nov 04 '20 at 01:07
  • 1
    According to the bottom of the [linqpad page](https://www.linqpad.net/), *"LINQPad’s Dump method is famous for its capacity to eat almost anything! Tuned with numerous heuristics, it intelligently walks object graphs for the best possible output. It even turns lazy objects into hyperlinks and asynchronous values into placeholders whose content materializes in the future!"*. Meanwhile, `Console.WriteLine` will simply call the `ToString` method of the object passed in (if it's not already a string). In your example, `Dump` wouldn't offer any advantage that I can see. – Rufus L Nov 04 '20 at 01:58

1 Answers1

2

Here is what LINQPad says about Dump:

LINQPad’s Dump method is famous for its capacity to eat almost anything!

Tuned with numerous heuristics, it intelligently walks object graphs for the best possible output. It even turns lazy objects into hyperlinks and asynchronous values into placeholders whose content materializes in the future!

Images and bitmaps render as images and bitmaps, and types from Reactive Extensions and Dataflow fully animate.

Dump any WPF or Windows Forms object and it will actually render.

And when you need a traditional data grid or debugger watch window, it’s there, too.

So, Dump() is an incredibly useful extension that will help us debug and understand how the code works, or what information is available to us. Dump() writes to the output window using LINQPad's output formatter and is overloaded to let us specify a heading and also specify a maximum recursion depth to override the default of 5 levels.

typeof (int).Assembly.Dump ("heading");
typeof (int).Assembly.Dump ("number of level we want to dump deep"); 

Surprisingly, there are a couple of parameters available for this method, such as description, depth, toDataGrid and exclude, that cause working with Dump to become more fun. They are more described in this Stack overflow page: here

Roksana
  • 264
  • 2
  • 16