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?)
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465

5 Answers5

278

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

Matthew Beck
  • 451
  • 5
  • 19
sloth
  • 99,095
  • 21
  • 171
  • 219
  • 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` – Colonel Panic Jun 21 '12 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. http://channel9.msdn.com/Series/C9-Lectures-Dr-Don-Syme-Introduction-to-F-/C9-Lectures-Dr-Don-Syme-Introduction-to-F-2-of-3 – John Dhom Jun 23 '12 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 '12 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 '16 at 01: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"` – mostafa8026 Mar 21 '18 at 07:39
  • @sloth Wow, Initialize Interactive with Project → This is gold, kindly also add screenshot of it so new people will easily able to understand. – Chandraprakash Sep 01 '21 at 05:25
  • Keep in mind that if you want to use `C# Interactive` with `.Net Core` sometimes you need to run `#reset core`. – stanimirsp Dec 15 '22 at 08:11
  • It seems to depend on the type of project you are using. I use `VS 2022` and have 2 `console application` project types. By one i can only use old .NET versions and are able to interact with my code with the `Initialize Interactive with Project` option. With the other `console application` i can use recent versions of .Net and need to use the `reference to .dll` method. – CodeNinja May 23 '23 at 09:50
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

Botz3000
  • 39,020
  • 8
  • 103
  • 127
  • 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. – Colonel Panic Jun 21 '12 at 10:08
  • @MattHickford: What type of project is the project? We haven't done much testing beyond simple ConsoleApplications and ClassLibraries. – Kevin Pilch Jun 21 '12 at 15:18
  • Thanks for asking Kevin. I've just tried 'reset interactive from project' on some smaller projects, it worked right. The projects it doesn't work for reliably are more complicated libraries and applications with many references. – Colonel Panic Jun 21 '12 at 15:41
  • @MattHickford: what type of project were you doing it on? As Kevin said we haven't tested this but it'd be good to know where we do already fall over. – Jason Malinowski Jun 21 '12 at 16:34
  • @MattHickford: I got the 'Invalid assembly name' error when the project/solution has compile errors. – John Dhom Jun 23 '12 at 20:03
  • 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 '16 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). – Andrei Pana Aug 15 '16 at 14:00
  • @ygoe There is a bug on GitHub. If you have more info, please help them debug it: https://github.com/dotnet/roslyn/issues/21604 – m93a Dec 18 '17 at 11:04
  • This is not a general solution, it might work for some people. – Gavin Williams Mar 19 '21 at 14:04
  • 1
    None of this exists in Visual studio 2022 sadly (Reset nor Initialize with Interactive) :( – Richard Griffiths Oct 25 '22 at 09:26
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.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Luke Hammer
  • 2,076
  • 3
  • 18
  • 31
  • 11
    No it's not. I've got it in exactly the same spot as the screenshot points out. – Tom Apr 10 '17 at 09: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? – Luke Hammer Apr 26 '17 at 20:01
  • @WizardHammer I believe Class Library – Tom Apr 27 '17 at 07: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 '17 at 09:37
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.

Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149
Mahmoud Hanafy
  • 1,103
  • 12
  • 12
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;
Tim Sylvester
  • 22,897
  • 2
  • 80
  • 94
beloblotskiy
  • 948
  • 9
  • 7