Scott Hanselman

Interactive Coding with C# and F# REPLs (ScriptCS or the Visual Studio Interactive Window)

January 29, 2016 Comment on this post [37] Posted in Open Source
Sponsored By

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.

The C# Interactive Shell CSI

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.

C# Interactive REPL inside Visual Studio

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.

Introducing the Visual Studio 'C# 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.

The F# Interactive Shell

F#'s REPL is also inside VS, right here next to the C# Interactive Window.

C# Interactive and F# Interactive in VS

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.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
January 29, 2016 13:00
Whats the difference between the VisualStudio c# interactive window and VScode's equivalent.
The following (C#6 syntax) code works in Visual Studio's c# interactive window but not in VScode.


var x = "Timbo";
Console.WriteLine($"Hello {x}")


I get the error

CS1056: Unexpected character '$'

Tim
January 29, 2016 13:18
Good news. I have been using LinqPad for REPL, it's not really what it's designed for, but it works quite well.
January 29, 2016 13:35
Scriptcs is awesome. I remember the tweet discussion where the idea was brought up by Glenn Block on twitter, and the first versions coming out, Nancy apps demo showcased quickly after :). OSS at its best.

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!
January 29, 2016 14:23
Have you seen CShell which is also an open source C# REPL loop (with active community on GitHub), although it's not integrated with VS it's pretty powerful and is very full featured.
January 29, 2016 14:51
What would help a lot is if I could "install" NuGet packages into the REPL.

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.
January 29, 2016 17:05
I being using mono's REPL for years now. It's an amazing, reliable, fast piece of software. (The same is true for the whole mono toolchain: xbuild, mcs, monodis et.al).
January 29, 2016 17:20
The Mono REPL works out the box and I use it daily. I've been using ScriptCS for a couple of years. It wasn't so simple to setup before but it was worth the hassle just to avoid the chore of creating a project just to test something really simple like parsing a Date object.

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 :-)
January 29, 2016 18:31
This isn't quite REPL related, but it involves ScriptCS. We're developing an application that generates reports in a background process. We were in the process of developing reports and the reporting framework at the same time. So although our reports would run and the resulting bytes would be stored in our SQL Server database, we had no UI to pull the files down and verify them.

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?!
January 29, 2016 18:38
Why are there 3 visual studio windows one for f#, one for c# and one for powershell?

It should be one cmdprompt with toggle mode.

Such a mess
January 29, 2016 19:28
I remember using IronPython for a good chunk of time as a REPL for debugging some C# class libraries I was working on at the time. It was quite handy for that at the time. Since then I've sometimes used FSI for similar needs.
Max
January 29, 2016 21:49
There's also a web based C# REPL at http://csharppad.com , which is useful if you need to quickly evaluate some code or send a link to a friend.

[Full disclosure: I'm the author]
January 29, 2016 22:07
Yes! About time we have a native REPL inside Visual Studio. Huzzah!
January 30, 2016 0:42
This was how I learned to program in 1982... on a CP/M-80 computer running MSBASIC. :-)
January 30, 2016 1:36
Great post. I love the addition to VS2015 Update 1.

Possible typo?

You have: VS ships a command line RELP called "CSI"

should it be "REPL" instead?
January 30, 2016 3:06
And where's VB's REPL?
February 01, 2016 9:32
Another tool to add and make your large system into ravioli code: https://en.wikipedia.org/wiki/Spaghetti_code

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.
ted
February 01, 2016 17:18
If you are keeping track of how to send the code from the script to the REPL in Visual Studio:
F#: ALT + ENTER
C#: CTRL + ENTER
Python: CTRL + E, E
R: CTRL + ENTER

Too bad it is not consistent.
February 01, 2016 23:04
It looks like the interpreter might not be returning all results?

var i = "scott";

Should return "scott", I thought?
February 02, 2016 19:37
Nice! I've been using LinqPad for years.
February 03, 2016 14:12
Any chance of getting a stand-alone installer for CSI.exe?

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...
February 04, 2016 1:45
Hi MarcelDevG,

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
February 04, 2016 12:32
Worthy man,keep doing this great work.Developing an android or ios application is not a back-breaking process,I got a completely developed application pleasing my requirements in four to five working days and was also provided with tutorials plus source code.
February 05, 2016 0:31
@Leak

That's what ScriptCS is for!
February 05, 2016 13:14
Channel 9
February 05, 2016 14:09
@Mason McGlothlin: where's the MSI package for that? I'm not going to install some random other PowerShell package (choco-what?) just to download and install something else when the OS has built-in standard facilities for this, especially not on a production server.
February 07, 2016 15:01
Nice. Thank you for sharing.
February 08, 2016 0:23
REPLs are great indeed! I use the F# REPL day in, day out - for everything, ranging from rapid prototyping of a domain model (more or less as a replacement for classic TDD), to exploring an API or a library interactively, working with data (not having to reload a dataset all the time between code changes is awesome), or simple, dumb scripts (ex push data to a blob on Azure). If anybody is interested, this post reminded me that I found out a lot of small 'F# scripting tricks' over time, so I put them all in one post here: http://brandewinder.com/2016/02/06/10-fsharp-scripting-tips/ . Cheers!
February 08, 2016 3:21
Thanks Anthony for replying.
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...).

February 08, 2016 18:53
Control hardware from C# REPL

https://www.youtube.com/watch?v=JM8RPIR2pVg
February 09, 2016 22:53
So my first thought is to use Razor to replace T4? Anyone tried this or interested in collaborating on it? I have a little project that has a bunch of T4 that I am going to restructure, this would be a great time for me to see if it can be done.
February 11, 2016 8:51
@Tim hey, if I don't get wrong and you wanna have output as
> 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!
February 11, 2016 13:03
Hi Ozan,
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)
Tim
February 11, 2016 23:21
Hi again Tim,
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!
February 15, 2016 8:32
Note that you can script .NET from IronPython and PythonNET. The latter even works with Jupyter notebooks.

Scriptcs was integrated as a C# kernel to Jupyter:

https://github.com/zabirauf/icsharp

Not sure if this is possible with csi from roslyn.
February 17, 2016 16:08
I've used LinqPad for years
February 17, 2016 18:55
I really loved this post.

Full of useful information.

Best regards everybody!
March 07, 2016 19:10
I do believe all of the ideas you have introduced to your post.
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.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.