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...
11 Replies
Okay, Maybe there is some kind of alternative that can be used for this? Some nuget package?
Just... make your own method to do this 🙂
seems a little overkill to get a nuget package for it
string.join is your friend~
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?
Write one for IEnumerable?
IEnumerable<IEnumerable<IEnumerable<char>>>
if item is IEnumerable -> Call recursively.
call recursively if it's nested?
yeah
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/458354Stack 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...
You can use these methods in a normal .NET project.
@Кён Кёныч, more details here: https://github.com/dotnet/interactive/blob/main/docs/formatting.md
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...
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: