In Visual Studio 2015 or later, I can open the ‘C# interactive window’, and run code:
> 5 + 3
8
That’s cute. Now how can I interact my code—my classes? Assume I have a project open.
> new Cog()
(1,5): error CS0246: The type or namespace name 'Cog' could not be found (are you missing a using directive or an assembly reference?)
For the latest cross-platform .NET Core/Standard/6/7/… assemblies:
These assemblies are NOT supported by Visual Studio’s Initialize Interactive with Project feature per open Roslyn work item here.
Also, Visual Studio versions before 2015 do not support this feature at all.
References to .NET Core assemblies (such as .dll files) can be added to the C# Interactive Window by using the
#rcommand in the C# Interactive window.Here is an example usage of the #r command:
After running the above command (with the correct DLL path) in the C# Interactive window, the following line will work:
Alternative Solution: LINQPad
For .NET Framework projects in Visual Studio between 2015 and 2022:
You can open the Interactive window by navigating to Views > Other Windows > C# Interactive,
Then just right click your project and run Initialize Interactive with Project from the context menu.
Alternative Solution: Immediate Window