Click here to Skip to main content
15,896,063 members
Articles / Programming Languages / C#

Prototyping with C#? Thanks, Roslyn!

Rate me:
Please Sign up or sign in to vote.
4.56/5 (6 votes)
23 Apr 2014CPOL2 min read 12.9K   13   1
Prototyping with C#? Thanks, Roslyn!

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:

image

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 within 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.

  1. Win + R
  2. ‘cmd’
  3. scriptcs

Boom, you’re sitting in the ScriptCS C# interpreter. Code away!

C#
Console.WriteLine("Hello, World!");

And profit.

image

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:

C#
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:

image

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!

image

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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I'm a Sr. Software Engineer in the Seattle area primarily focused on serverless technologies in the cloud. In my free time I enjoy hiking & other adventures with my family around the Puget Sound and the country! You can find out more about me at my homepage: http://bc3.tech/brandonh

Comments and Discussions

 
GeneralMy vote of 5 Pin
Volynsky Alex23-Apr-14 8:55
professionalVolynsky Alex23-Apr-14 8:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.