Long before Roslyn was a thing at this years’ BUILD developer conference, it was being used by a small group of Microsofties to create something pretty sweet.
This thing allowed you to write C# at the command line. So yeah, think Python, but with C#. Quick to get started, quick to work with, quick to execute. In this post, I’m going to introduce you to – and show off – ScriptCS.
First thing’s first. Let’s get it installed. To do this I’ll direct you to my ol’ faithful Chocolatey to make this happen with extreme ease. If you haven’t installed Chocolatey yet, go ahead and do that – I’ll wait the usual 10 seconds :)
Once you’ve got Chocolatey installed, getting ScriptCS is as easy as
cinst scriptcs
and voila. Now you’re all set. If you paid attention to what Chocolatey output while it was doing the install, you’ll see why we have Roslyn to thank for this beauty of a tool:
If you visit the ScriptCS website, a lot of what they’ll show you is how to write ScriptCS files using your favorite text editor, then executing them at the command line. That’s all well and good but honestly I don’t see how that’s any different than firing up VS or VS Express, writing a CS file, and cracking it off right w/in the IDE.
What’s valuable to me is firing up a command prompt and writing C# to test something. That quick. I work on an SDK at work and sometimes I want to just quickly verify that the SDK is doing what I expect. To this end, let’s see how easy ScriptCS makes this.
Let’s do a simple Hello World application without running any application other than our Command Prompt.
- Win + R
- ‘cmd’
- ‘scriptcs’
Boom, you’re sitting in the ScriptCS C# interpreter. Code away!
Console.WriteLine(“Hello, World!”);
And profit.
How hot is that? The beauty of this is imagine you have written a DLL or some other reusable component and wanted to check out how the changes you made work really quickly and easily. Adding and using references, even nuget ones, can also be done right w/in the ScriptCS interpreter.
I created one Class Library project with this as its contents:
using System;
namespace ClassLibrary1
{
public class Class1
{
public void PrintNumbers()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("#{0}", i);
}
}
}
}
Easy enough, build in to ClassLibrary1.dll, copy to c:\users\brandon and off I go:
Quickly and easily I just prototyped my library and played around a bit.
I remember early versions of ScriptCS and it didn’t used to allow multi-line REPL. After some back and forth with Glenn Block (one of the founders) and a couple of the other team members it was brought to my attention that indeed it does now!
So there you have it! Quick, easy, C# with nothing more than a command line! Be sure to check out the ScriptCS website and Github project area to learn more about what you can do with it and how to do it!