Interactive Coding with C# and F# REPLs (ScriptCS or the Visual Studio Interactive Window)
REPLs are great! REPL stands for Read–eval–print loop and is pronounced "REP-L" quickly, like "battle." Lots of languages and environments have interactive coding and REPLS at their heart and have for years. C# and F# do also, but a lot of people don't realize there are REPLs available!
ScriptCS
In 2013 once the Roslyn open source C# compiler started to mature, Glenn Block and many friends made ScriptCS. It now lives at http://scriptcs.net and has a great GitHub and active community. The Mono project has also had a REPL for a very long time.
You can install ScriptCS in minutes with the Chocolatey Package Manager or OneGet with Chocolatey on Windows 10. In the screenshot above I'm writing code at the command prompt, making mistakes, and fixing them. It's a great way to learn and play with C#, but it's also VERY powerful. You can create C# Scripts (.csx files) kind of like PowerShell but it's just C#!
Visual Studio's REPLs - CSI and FSI
The Visual Studio team meets/met with the ScriptCS folks in the open and even publishes their meeting notes on GitHub! In May of last year they got ScriptCS working in OmniSharp and Visual Studio Code, which is amazing. There's a great set of directions here on how to set up ScriptCS in Visual Studio Code and the code is moving fast on GitHub.
Visual Studio 2015 Update 1 has REPLs within the IDE itself. If you have Visual Studio 2015, make sure you've updated to Update 1. If you don't have VS, you can get the free Visual Studio Community at http://visualstudio.com/free.
VS ships a command line RELP called "CSI" that you can use to run ".csx" scripts as well. Turns out the source code for CSI is basically nothing! Check it out at http://source.roslyn.io/#csi/Csi.cs and you can see how easy it would be for you to add scripting (interactive or otherwise) to your own app.
There's a great C# Interactive Walkthrough by Kasey Uhlenhuth that you should take a moment and play with. She's the Program Manager on this feature and also has a great video on Channel 9 on how to use the C# Interactive REPL.
Of course, F# has always had a REPL called "fsi.exe" that also ships with VS. You may have this on your PATH and not realize it, in fact. F# script files are ".fsx" so there's a nice symmetry with scripting and REPLs available in both languages, either in VS itself, or at the command line.
F#'s REPL is also inside VS, right here next to the C# Interactive Window.
These are all great options for learning and exploring code in a more interactive way than the traditional "write, compile, wait, run" that so many of us are used to.
Let's hear in the comments how (or if!) you're using REPLs like these two make your programming life better.
Sponsor: Big thanks to Wiwet for sponsoring the feed this week. Build responsive ASP.NET web appsquickly and easily using C# or VB for any device in 1 minute. Wiwet ASP.Net templates are integrated into Visual Studio for ease of use. Get them now at Wiwet.com.
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
Now, it's getting integrated in VS and the editors, that's awesome, I think that it will really shine when you can start scriptcs REPL and poke at a running appdomain :) looking forward to see that!
E.g. today to play with Rx I have to do following:
#r "somewhere\Packages\Rx-Core.2.2.5\lib\net45\System.Reactive.Core.dll"
#r "somewhere\Rx-Interfaces.2.2.5\lib\net45\System.Reactive.Interfaces.dll"
#r "somewhere\Rx-Linq.2.2.5\lib\net45\System.Reactive.Linq.dll"
And before that I have to make sure that I installed Rx packages somewhere.
But what I actually would like to do is something like:
#nuget "Rx.Main" "2.2.5"
And REPL should install and reference the packages.
Otherwise it is really helpful and I use LinqPad less and less.
One question though, does anyone know how to clear the screen? Once you reach the bottom of the terminal your stuck there and it would be nice to get rid of the noise above.
Cheers :-)
I built a C# script that would connect to SQL Server, find the latest report submitted, download it, and write it to a file, and launch the file with the default program for that file. Only took a few minutes to write, and was able to quickly get it to our entire team to use until our reporting UI was ready.
Since I was distributing a script instead of a compiled program, it was simple to modify in response to the changing database schema. I enjoyed that I didn't need to learn Powershell. While PS is nice, why spend the time learning it if I can be productive in the main language I program in?
All of this would have been unnecessary if SQL Server Management Studio had an easier mechanism for opening binary files. When will that be open sourced?!
It should be one cmdprompt with toggle mode.
Such a mess
[Full disclosure: I'm the author]
Possible typo?
You have: VS ships a command line RELP called "CSI"
should it be "REPL" instead?
Minimum viable product feature set, minimum viable toolset and minimum viable platform come to mind.
Buying a car for it to only last until you get it off the dealer's lot or buying a car for total cost of ownership over the expected lifespan of the car.
F#: ALT + ENTER
C#: CTRL + ENTER
Python: CTRL + E, E
R: CTRL + ENTER
Too bad it is not consistent.
var i = "scott";
Should return "scott", I thought?
When dealing with stuff like ad-hoc Sharepoint cleanup or update jobs I'd be much happier to use the server side object model instead of PowerShell, but installing VS 2015 U1 just for that on a production server is kinda overkill...
We started work on the VB REPL a little later after the major pieces of the design worked out. I've done demos of the code internally but it just wasn't at ship quality for Update 1. Right now we've switched gears back toward VB 15 and C# 7 language features to make sure they have as much bake time as possible but I definitely want us to improve the interactive experience for Visual Basic developers ASAP.
In the meantime, there are a few options to play with Visual Basic interactively.
ScriptCS added VB support based on the VB scripting API in Roslyn:
https://github.com/scriptcs/scriptcs/tree/dev/src/ScriptCs.VisualBasic
Also, VB has historically had an interactive prompt through the Immediate Window. You may be familiar with the Immediate window at debug time but since 2005 you've been able to use it to evaluate VB expressions and statements at design time. There are pros and cons to this over what shipped in the C# REPL today that are kinda lengthy and nuanced but I'd love your feedback on how to make you most productive.
A lot of people have said, and I agree, that in the long-term unifying the experiences between the Immediate and Interactive windows would be ideal but there's a lengthy road to get there and your feedback would be helpful in prioritizing how we get there. You can leave your feedback here, on UserVoice, on the Roslyn github page, or by using the contact form on the VB team blog.
Regards,
Anthony D. Green, Program Manager, Visual Basic
The immediate windows falls a bit short as a REPL.
I mostly start a console app for trying out code, or Linqpad.
It would be great to have a full REPL in VS2015.
VB is on the backburner again :(. (ASP5/ASP Core/REPL etc...).
> Hello Timbo
also you have two mistake:
First; as you said; '$' is an unexpected character in your statement and you can correct it as Console.WriteLine("Hello {x});
Second; the statement, which is fix your error, doesnt give you output "Hello Timbo" actually it gives "Hello {x}" output and you need to change it as > Console.WriteLine("Hello {0}", x); For more information, search for "c# string placeholder".
Have good time!
no my syntax is correct i believe, as i put in my comment its c#6 syntax. That was the point of my query. (https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6#string-interpolation)
sorry I've missed your info note. I also check it again and run the exact same code you wrote and it works as you expect. Can you confirm that you have VS 2015 Update 1 as shown in the part of this blog post: http://s10.postimg.org/56lu5w26x/image.png
Greetings!
Scriptcs was integrated as a C# kernel to Jupyter:
https://github.com/zabirauf/icsharp
Not sure if this is possible with csi from roslyn.
They're very convincing and will definitely work. Still, the
posts are too brief for starters. Could you please prolong them a
little from next time? Thank you for the post.
Comments are closed.
The following (C#6 syntax) code works in Visual Studio's c# interactive window but not in VScode.
I get the error
CS1056: Unexpected character '$'