Can the C# interactive window interact with my code?

Colonel Panic picture Colonel Panic · Jun 21, 2012 · Viewed 49.5k times · Source

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?)

Answer

sloth picture sloth · Jun 21, 2012

When using Visual Studio 2015:

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.


For older versions:

To use the C# Interactive Window with your code, ensure you first compile your code, then add a reference to the resulting assembly with the #r command like so:

C# command prompt


You can also use the Immediate Window to interact with your code as well, like so:

Immediate Window