Method for printing collections.

So .NET Interactive has a display function (https://github.com/dotnet/interactive/blob/main/docs/display-output-csharp.md) that allows to nicely print collections and even iterators in Notebooks. So I wonder, is it possible to access this method in a normal C# project to print collections to stdout.
GitHub
interactive/docs/display-output-csharp.md at main · dotnet/interact...
.NET Interactive combines the power of .NET with many other languages to create notebooks, REPLs, and embedded coding experiences. Share code, explore data, write, and learn across your apps in way...
No description
11 Replies
Кён Кёныч
Кён КёнычOP12mo ago
Okay, Maybe there is some kind of alternative that can be used for this? Some nuget package?
Lisa
Lisa12mo ago
Just... make your own method to do this 🙂 seems a little overkill to get a nuget package for it string.join is your friend~
Кён Кёныч
Кён КёнычOP12mo ago
I will have to write a method to handle EVERY collection in stdlib, nested collections, etc. I mean, it's possible but why reinventing wheels?
Lisa
Lisa12mo ago
Write one for IEnumerable?
Кён Кёныч
Кён КёнычOP12mo ago
IEnumerable<IEnumerable<IEnumerable<char>>>
Lisa
Lisa12mo ago
if item is IEnumerable -> Call recursively.
Chiyoko_S
Chiyoko_S12mo ago
call recursively if it's nested? yeah
mldisibio
mldisibio12mo ago
I don't recommend complex approaches with funky dependencies, but in your case, if you are looking to display any variety of collection or object graphs, this is well-known feature of LinqPad Dump() that is so popular people ask to incorporate it into Visual Studio or c# projects. Since LinqPad comes with a standalone executable, it's an option, albeit somewhat convoluted: https://stackoverflow.com/a/73541726/458354
Stack Overflow
Calling LinqPad 7 from Visual Studio C# Project
I am trying to call LinqPad query from C#. Unfortunately, the code below does not work; the result is null as if nothing got returned by the script. I don't see any example of how to do this online...
jonsequitur
jonsequitur12mo ago
You can use these methods in a normal .NET project.
jonsequitur
jonsequitur12mo ago
GitHub
interactive/docs/formatting.md at main · dotnet/interactive
.NET Interactive combines the power of .NET with many other languages to create notebooks, REPLs, and embedded coding experiences. Share code, explore data, write, and learn across your apps in way...
jonsequitur
jonsequitur12mo ago
The Display methods do two things. They call a formatter and get back one or more strings (for different MIME type representations of the object being formatted) and then they emit an event containing those formatted values. In a normal .NET project you're probably not using a kernel or listening to the events, but doing step 1 is simple. It's just up to you to figure out how to display that value. The simplest example looks like this:
var formatted = myObject.ToDisplayString("text/html");
var formatted = myObject.ToDisplayString("text/html");
Want results from more Discord servers?
Add your server