Dotnet logo

.NET Tools

Essential productivity kit for .NET and game developers

How-To's

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.

Reference assembly in Rider C# Interactive

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.

Reference project in current solution in C# Interactive

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.

Build project when using "Reference in C# Interactive"

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.

Invoke code from C# Interactive

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!

Debugging code 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!

image description