Add references and debug code snippets in Rider C# Interactive
We have made some updates to the C# Interactive tool window in the latest Rider 2018.2 EAP build! It’s now much easier to add project and assembly references to the current C# Interactive session. And once a project reference is added, we can attach the debugger and debug code snippets we are working on! Let’s see how this all works.
Add project and assembly references in C# Interactive
The
csi.exe tool (or the
csharp tool on Mono) both support referencing additional assemblies. This can be done using the reference syntax.
// C# Interactive csi.exe #r "C:\Users\Maarten\.nuget\packages\newtonsoft.json\11.0.2\lib\netstandard2.0\Newtonsoft.Json.dll" // Mono csharp executable LoadAssembly ("C:\Users\Maarten\.nuget\packages\newtonsoft.json\11.0.2\lib\netstandard2.0\Newtonsoft.Json.dll");
While not rocket science, it’s not very convenient to have to look up the full path to an assembly that is being used by our project, and then write out the full command to load it.
In Rider 2018.2, we are making this easier: for projects and assemblies, a new Reference in C# Interactive context menu is available which will do just that: reference the selected project or assembly in C# Interactive. Rider will generate the load statement for us, and execute it at once.
The C# Interactive tool window also comes with its own toolbar button to help reference projects in the current solution. This makes it easy to reference a specific project and write some interactive code against it.
By default, Rider will use the currently built version of the project’s assembly. To make sure the referenced project assembly is always up-to-date, we can configure Rider (under Languages & Frameworks | C# Interactive) to build the project when using the Reference in C# Interactive action.
Debugging code snippets with C# Interactive
C# Interactive is very useful to quickly run C# statements in a read-eval-print loop (REPL) without having to wait for full compilation, and get immediate feedback on what a given expression will return. And when referencing our own project, we can easily create classes and invoke methods from our own code base and see what they return.
Things get trickier if we want to see what happens during execution. Did we step into that
if statement in our code? What is the value of the variable
who during execution? Rider 2018.2 now supports attaching the debugger to C# Interactive and step through our own code when invoked from C# Interactive!
Note this only works on Windows for now. And perhaps also good to know is that this is not an Immediate Window for debugging our application process, as the debugger is attached to C# Interactive and not to our running application.
No more need to write a console application or a unit test to quickly debug a snippet of code. All we have to do is reference our project and start debugging it with C# Interactive!
What’s next?
There’s one thing still missing here right now: code completion in the C# Interactive tool window. We’re working on that, and hope to make it available in one of our next EAP builds.
Download Rider 2018.2 EAP and give it a try! We’d love to hear your feedback!
5 Responses to Add references and debug code snippets in Rider C# Interactive
Smad says:July 7, 2018
What about “Dump()”, setting default project and namespaces as in linqpad.
Maarten Balliauw says:July 7, 2018
Dump()is not part of the C# Interactive executable, however you could create a custom assembly that has a method that is able to dump object data to the console output. C# Interactive lacks the fancy visualizers LINQPad has, so it will have to be text being written.
That assembly can then be automatically referenced in
csi.exeby specifying it as an additional
/r:path-to-assembly.dlland corresponding
/u:namespaceto load it automatically.
Smad says:July 9, 2018
Thx, it was very useful.
Daniel B says:September 4, 2018
can you use the C# interactive when debugging?
e.g. debugger hits a breakpoint, then can query using the C# interactive tool.
Maarten Balliauw says:September 4, 2018
No, that’s a feature in our backlog – check if you want to upvote