205

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?)
5
  • 3
    This is one feature shipped with Roslyn blogs.msdn.com/b/csharpfaq/archive/2012/01/30/… Jun 21, 2012 at 9:57
  • I always use the standard Immediate window for this - works with your own types too.
    – Polyfun
    Jun 21, 2012 at 9:59
  • 2
    What is the 'standard immediate window?' Jun 21, 2012 at 9:59
  • for this, it's better to use Immediate Window Aug 5, 2016 at 0:54
  • Don't forget to change class name range, to public class name.<br/> :)
    – ndukan
    Apr 15, 2017 at 19:33

5 Answers 5

281

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 #r command in the C# Interactive window.

Here is an example usage of the #r command:

#r "C:\\path\\to\\your\DLL\\netstandard2.0\\Newtonsoft.Json.dll"

After running the above command (with the correct DLL path) in the C# Interactive window, the following line will work:

using Newtonsoft.Json;

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

Immediate Window

8
  • 2
    Thanks. I like the C# interactive window better. The immediate window is unfriendly, there's no autocomplete. Console.WriteLine("hey") The name 'Console' does not exist in the current context System.Console.WriteLine("hey") Expression has been evaluated and has no value Jun 21, 2012 at 12:54
  • -1 the question is about using the new C# REPL (interactive window) that is part of roslyn. Have a look at the Don Syme's c9 videos where he uses the F# Interactive Window as he develops. Good stuff. channel9.msdn.com/Series/…
    – John Dhom
    Jun 23, 2012 at 20:06
  • 36
    @JohnDhom Why the downvote? My answers totally shows how to interact with your own code via the C# interactive window (there's even a screenshot showing that).
    – sloth
    Jun 29, 2012 at 12:41
  • 16
    In .Net Core projects, the context menu item doesn't appear, so the #r method is required. This answer is perfect for that.
    – Ryan
    Nov 18, 2016 at 1:01
  • 6
    I want to add one more useful hint: if you want to use some run-time things, you can easily add this directive: #r "System.Runtime" Mar 21, 2018 at 7:39
70

You can use classes from your own project.
Just right click on your solution and select "Reset Interactive from Project".

If you need more information, here is the source:
Using the C# Interactive Window that comes with Roslyn – Part 2

10
  • 1
    Thanks, that sounds right. Alas, it doesn't work as described—whatever project I try that on, it pops up an error alert saying 'invalid assembly name'. Frustratingly, it doesn't explain what assembly it's talking about. I guess the software is broken. Jun 21, 2012 at 10:08
  • @MattHickford: What type of project is the project? We haven't done much testing beyond simple ConsoleApplications and ClassLibraries. Jun 21, 2012 at 15:18
  • 26
    None of this applies to VS 2015 anymore. The context menu command is gone and #r doesn't find my project assemblies. Any ideas?
    – ygoe
    Jan 22, 2016 at 14:26
  • 18
    In VS 2015 Update 3, there is a 'Initialize Interactive with Project' when right clicking a project in the solution (in Solution Explorer). Aug 15, 2016 at 14:00
  • 1
    None of this exists in Visual studio 2022 sadly (Reset nor Initialize with Interactive) :( Oct 25, 2022 at 9:26
55

It's worth noting that the feature isn't yet supported in VS 2019 for .Net Core project.

You won't find the option, and it's a known issue as highlighted in this answer "Initialize interactive with Project" is missing for .Net Core Projects in Visual Studio 2019

The workaround is to use #r command (#r "Path/MyDll.dll") to load the assembly manually as seen in the answer above.

2
  • 7
    Still be the case in VS2022 with dotnet6. We'll have to patiently await the feature to be reintroduced. Nov 18, 2021 at 9:43
  • 1
    @ChrisJensen Don't want to steal your thunder, but that would make a good Answer...
    – NWoodsman
    Feb 23, 2022 at 15:57
55

Just an update from the @Botz3000 answer.

The command you want to find is now called "Initialize Interactive with Project"

enter image description here

Also it is worth noting i could not find this command if my C# interactive window was not viewable.

4
  • 11
    No it's not. I've got it in exactly the same spot as the screenshot points out.
    – Tom
    Apr 10, 2017 at 9:37
  • 4
    @Tom it seems to function or not function based on what type of project you are working with. what was the project type you were working with when you found it? Apr 26, 2017 at 20:01
  • @WizardHammer I believe Class Library
    – Tom
    Apr 27, 2017 at 7:15
  • 19
    I am trying to get this to work with a NetStandard library and the option is not there in the context menu
    – ChadT
    May 31, 2017 at 9:37
21

Totally agree "Initialize Interactive with Project" is cool.

My approach is to push classes into a library and use //css_reference in C# script or #r in C# Interactive window

For example:

#r "D:\\dev\\DbHMonData\\LoadH2Stats\\bin\\Debug\\DbHMonStats.dll"
using DbHMonStats;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.