Scott Hanselman

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

January 29, '16 Comments [42] 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
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Friday, 29 January 2016 09:00:55 UTC
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
Friday, 29 January 2016 09:18:08 UTC
Good news. I have been using LinqPad for REPL, it's not really what it's designed for, but it works quite well.
Palle Due Larsen
Friday, 29 January 2016 09:35:09 UTC
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!
Friday, 29 January 2016 10:23:24 UTC
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.
Friday, 29 January 2016 10:51:41 UTC
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.
Konstantin Savelyev
Friday, 29 January 2016 13:05:50 UTC
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).
Friday, 29 January 2016 13:20:19 UTC
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 :-)
Friday, 29 January 2016 14:31:32 UTC
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?!
Friday, 29 January 2016 14:38:09 UTC
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
rajiv
Friday, 29 January 2016 15:28:51 UTC
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.
Friday, 29 January 2016 17:49:30 UTC
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]
Sunny
Friday, 29 January 2016 18:07:04 UTC
Yes! About time we have a native REPL inside Visual Studio. Huzzah!
William Denman
Friday, 29 January 2016 20:42:41 UTC
This was how I learned to program in 1982... on a CP/M-80 computer running MSBASIC. :-)
Minnesota Steve
Friday, 29 January 2016 21:36:02 UTC
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?
Friday, 29 January 2016 23:06:51 UTC
And where's VB's REPL?
MarcelDevG
Monday, 01 February 2016 05:32:14 UTC
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
Monday, 01 February 2016 13:18:10 UTC
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.
Monday, 01 February 2016 19:04:33 UTC
It looks like the interpreter might not be returning all results?

var i = "scott";

Should return "scott", I thought?
Charlie
Tuesday, 02 February 2016 15:37:13 UTC
Nice! I've been using LinqPad for years.
Rich
Wednesday, 03 February 2016 10:12:29 UTC
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...
Leak
Wednesday, 03 February 2016 21:45:32 UTC
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
Thursday, 04 February 2016 08:32:03 UTC
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.
Thursday, 04 February 2016 20:31:41 UTC
@Leak

That's what ScriptCS is for!
Friday, 05 February 2016 09:14:37 UTC
Channel 9
Jason palmer
Friday, 05 February 2016 10:09:53 UTC
@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.
Leak
Sunday, 07 February 2016 11:01:50 UTC
Nice. Thank you for sharing.
Sunday, 07 February 2016 20:23:52 UTC
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!
Sunday, 07 February 2016 23:21:12 UTC
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...).

MarcelDevG
Monday, 08 February 2016 14:53:15 UTC
Control hardware from C# REPL

https://www.youtube.com/watch?v=JM8RPIR2pVg
Tuesday, 09 February 2016 18:53:39 UTC
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.
Thursday, 11 February 2016 04:51:39 UTC
@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!
Ozan Topal
Thursday, 11 February 2016 09:03:55 UTC
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
Thursday, 11 February 2016 19:21:41 UTC
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!
Ozan Topal
Monday, 15 February 2016 04:32:50 UTC
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.
Wednesday, 17 February 2016 12:08:51 UTC
I've used LinqPad for years
Rich Nahra
Wednesday, 17 February 2016 14:55:29 UTC
I really loved this post.

Full of useful information.

Best regards everybody!
Josue Martinez Buenrrostro
Tuesday, 01 March 2016 21:50:48 UTC
I can't approach this steps in my kernel. Could anyone help me?
<a href="http://mobdroappdownloads.com/mobdro-iphone-ipad-ios/" rel"nofollow">mobdro for ios</a>
or
<a href="https://mobdroios.com" rel"nofollow">mobdro app for iPhone</a>
Mobdro
Tuesday, 01 March 2016 21:51:49 UTC
I can't approach this steps in my kernel. Could anyone help me?
mobdro for ios
or
mobdro app for iPhone
Mobdro
Monday, 07 March 2016 15:10:31 UTC
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.
Tuesday, 08 March 2016 21:30:25 UTC
Mobdro application for android is among the unquestionably best sources for cartoon, with vast assortment of movies this application may bring back plenty of recollections.
Using the Mobdro apk Android , it may be loved on the run!
Mpbdro Free App is a best application that provide amazing feature of watching Live TV.
Comments are closed.

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