The C# Interactive window allows you to use a C# REPL that can be pretty useful for testing something out quickly. I’ve used it a bunch of times for formatting a DateTime to a string, because I can never quite remember which combination of characters results in what I want.
In Visual Studio, you can find the C# Interactive Window by selecting
View –> Other Windows –> C# Interactive
Anything included in the System namespace is available by default, but what about other libraries?
Importing A Project
You can import your own project to the C# Interactive Window, allowing you to interact with your code on the fly. This is particularly useful if you want to test something quickly (although, realistically you should be adding value and writing a unit test). Recently, a project I was working on (that I didn’t write) had a static function that would convert one value to a different value. I was digging around in log files, and I had one value in one place, but needed to find the converted value so I could continue spelunking.
The syntax for importing a project is really simple:
#r “C:\path\to\binary.dll”
The bummer about this is that you have to know the full path to the binary. It is kind of annoying, but in Visual Studio you can quickly find the path by right clicking on the project you want and selecting “Rebuild”, and then watching the Output window. The outputted binary will be displayed there, with its full path:
1>—— Rebuild All started: Project: ConsoleApp8, Configuration: Debug Any CPU ——
1>ConsoleApp8 -> C:\Users\robert\source\repos\ConsoleApp8\ConsoleApp8\bin\Debug\netcoreapp2.1\ConsoleApp8.dll
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========