1

Back in my VB6 days, I could use the Immediate Window to run a quick sanity test on a function, even if the rest of my code was in a broken state:

enter image description here

It was handy for quick and dirty REPL debugging.

When writing C# code in Visual Studio, I can't call any of my code from the Immediate Window unless all of it is valid and compiles.

That works fine when my project is in a ready to run state, but not when I'm in the midst of tearing apart a bunch of code.

Is there any way I can configure the Immediate Window to only compile what's needed for the statement I'm trying to execute? Or other workarounds?

I've heard LINQPad is awesome, but I assume it would require me to copy-paste all the code my test statement depends on before I can run it. I've also played with the newer Interactive Window but don't think it does what I'm hunting for.

rkagerer
  • 4,157
  • 1
  • 26
  • 29
  • My current 'solution' for this is commenting out the broken code, which sucks. – rkagerer Jun 06 '20 at 05:15
  • 1
    What's about excluding the file that it's breaking the build? Right click the file, select 'Exclude From Project' – ɐsɹǝʌ ǝɔıʌ Jun 06 '20 at 05:24
  • Import your last compiled dll's into LINQPad so you can access the functions inside it? – Zeb Rawnsley Jun 06 '20 at 06:20
  • What does this mean: "I've also played with the newer Interactive Window...."? – Jazimov Jun 06 '20 at 06:25
  • Maybe not the immediate window but **Ali Kianoor**'s answer below was actually on the right track. You can use _continuous testing_ tools like _nCrunch_ that can invoke your code even if you are working on a method or other aspects of your solution irrespective of whether your solution is in a compilable state or not. It will simply invoke the methods **that can be invoked** (passed compilation). If you are working on a method it will call it next all errors have been resolved. I believe the net result would be the same as the Immediate Window. R# has a similar feature –  Jun 06 '20 at 07:14
  • 1
    I don't really get your problem statement - you want to quick n dirty test something in isolation but also want it to somehow be able to access all of your code and run anything in it when the code is broken? – Caius Jard Jun 06 '20 at 10:00
  • Short of using some other tools like LINQpad or dotnetfiddle.net, I typically just keep a hello world console app open in the background, in another VS, stopped on a breakpoint and use the immediate window there. This is some conceptual equivalent of your VB6 - i don't have any reference but I presume that you could use a working module1 even though module2 is broken because it could run as interpreted as well as compiled, and it's like you can have javascript that is broken but doesn't cause any issue if it never runs, next to working JS that does run. Your .NET is compiled, and must all work – Caius Jard Jun 06 '20 at 10:06
  • @Jazimov: View | Other Windows | C# Interactive – rkagerer Jun 06 '20 at 19:24
  • @CaiusJard: Yes, just like your Javascript example. I want to use the Immediate Window (or a similarly lightweight scratchpad) to invoke a method in my project and only compile that method and those other bits of my code it depends on - ignoring errors in other parts of my code not required to run the function. Interpreted languages do this "for free", I assume VS tooling could do it with some dependency tree analysis (and heuristics to ignore broken code like unclosed blocks). – rkagerer Jun 06 '20 at 19:25
  • @MickyD: I can't see Ali Kianoor's answer could you point me to it? – rkagerer Jun 06 '20 at 19:26
  • He deleted it yesterday. It was talking about unit testing, which I guess he deemed isn't exactly what you were looking for – Caius Jard Jun 08 '20 at 03:39

1 Answers1

4

From your comment above, you already stumbled on the answer: The C# Interactive window ( View | Other Windows | C# Interactive) is the feature you want. If the Immediate Window isn't able to run the code you want to run due to existing project errors, then C# Interactive will. Visual Studio's C# Interactive window actually works the same way VB6's Immediate Window works. The C# Interactive window will successfully compile and run code that does not depend on your error-state project code:

enter image description here

And just like in VB6, you wouldn't be able to access a function or variable from project code that is not compiling--as others have pointed out, how would the compiler possibly work in any "immediate-window" mode if it had to depend on code that cannot compile? By as my image above proves, as long as the code you present to the C# Interactive window is compilable, it will work even when your project code will not.

enter image description here

As long as you steer clear of any code that might depend on the project code, you even can compile more complex examples, like this:

However, unlike VB6's Immediate window (which is a sort of VB-only hybrid of Visual Studio's Immediate along with Visual Studio's C#/F# Interactive window), you cannot reference values that exist in project code because the C#/F# Interactive window is sandboxed. Thus, in the examples shown, I cannot access ggc.Name in the C# Interactive window--that window doesn't have access to any project variables or state--whether or not the project has compilation errors.

Jazimov
  • 12,626
  • 9
  • 52
  • 59
  • Any chance for a better example other than `Hello`? –  Jun 06 '20 at 23:54
  • The point is that the C# Interactive window compiles code even when the current project has an error that prevents it from compiling. I added a more illustrative example. – Jazimov Jun 07 '20 at 06:52
  • 1
    Excellent. Now everyone knows. Thanks for the update +1 –  Jun 07 '20 at 06:54
  • Though if you want to use eg a class in it the class has to compile.. it's not(and will probably never be) exactly like the VB6 example. https://dzone.com/articles/c-interactive-in-visual-studio Has some usage tips - you can see how to send code to the InWin for checking, but everything it relies on needs to work (and importing references to the current project is a bit awkward) – Caius Jard Jun 07 '20 at 08:44
  • @CaiusJard: I supplemented my answer to give a bit more detail in the closing paragraph. – Jazimov Jun 07 '20 at 14:35
  • Thanks for this thorough answer. I did play with the Interactive Window, but you hit the nail on the head when you said _importing references to the current project is a bit awkward_. I assume the whole project being imported must compile? Ideally I was hoping there might be an option in the toolsuite to JIT-compile just the ad-hoc bits of my code that are needed, starting from the function I call, without having to manually figure out dependencies and copy/paste it all to a scratchpad or Interactive Window. – rkagerer Jun 08 '20 at 03:02
  • Yes; if you check the link out I posted you can see you can highlight some stuff and send it to the InWin and run it (but it has to compile on its own with no dependencies) and you can import reference to dlls but they have to compile. There seems, sadly, no way to have WorkingMethod and BrokenMethod in the same class, call WorkingMethod in the InWin and have VS select just it, plus any dependencies, for the sandbox and execute it. I don't really miss this aspect of the VB6 ImWin but I do find myself occasionally putting nuggets of code in another vs, which is what the InWin seems to be – Caius Jard Jun 08 '20 at 03:36