Can the C# interactive window interact with my code?
Asked Answered
E

5

209

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?)
Ecology answered 21/6, 2012 at 9:46 Comment(5)
This is one feature shipped with Roslyn blogs.msdn.com/b/csharpfaq/archive/2012/01/30/…Ecology
I always use the standard Immediate window for this - works with your own types too.Fistic
What is the 'standard immediate window?'Ecology
for this, it's better to use Immediate WindowApt
Don't forget to change class name range, to public class name.<br/> :)Knorring
A
287

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

Anticline answered 21/6, 2012 at 10:0 Comment(8)
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 valueEcology
-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/…Uphroe
@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).Anticline
In .Net Core projects, the context menu item doesn't appear, so the #r method is required. This answer is perfect for that.Hare
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"Radicand
@Anticline Wow, Initialize Interactive with Project → This is gold, kindly also add screenshot of it so new people will easily able to understand.Palgrave
Keep in mind that if you want to use C# Interactive with .Net Core sometimes you need to run #reset core.Rewire
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.Nucleo
G
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

Grained answered 21/6, 2012 at 10:4 Comment(10)
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.Ecology
@MattHickford: What type of project is the project? We haven't done much testing beyond simple ConsoleApplications and ClassLibraries.Egad
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.Ecology
@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.Chatterbox
@MattHickford: I got the 'Invalid assembly name' error when the project/solution has compile errors.Uphroe
None of this applies to VS 2015 anymore. The context menu command is gone and #r doesn't find my project assemblies. Any ideas?Catercornered
In VS 2015 Update 3, there is a 'Initialize Interactive with Project' when right clicking a project in the solution (in Solution Explorer).Elmiraelmo
@Catercornered There is a bug on GitHub. If you have more info, please help them debug it: github.com/dotnet/roslyn/issues/21604Grounder
This is not a general solution, it might work for some people.Septennial
None of this exists in Visual studio 2022 sadly (Reset nor Initialize with Interactive) :(Stephenstephenie
B
56

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.

Bricole answered 17/11, 2016 at 8:52 Comment(4)
No it's not. I've got it in exactly the same spot as the screenshot points out.Reflate
@Reflate 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?Bricole
@WizardHammer I believe Class LibraryReflate
I am trying to get this to work with a NetStandard library and the option is not there in the context menuBipartisan
D
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.

Dekameter answered 9/9, 2019 at 21:40 Comment(2)
Still be the case in VS2022 with dotnet6. We'll have to patiently await the feature to be reintroduced.Foxglove
@ChrisJensen Don't want to steal your thunder, but that would make a good Answer...Theological
M
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;
Myer answered 31/3, 2018 at 1:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.