Interactive C# and F# via the command line

Here I go again. I have some really simple (I mean, REALLY SIMPLE) C# code I want to try and I’m going to have to open Visual Studio, VS Code, LINQPad, or some other (relatively) heavyweight app just to run it.

You have the command line open.  Is there a way to run it from there?

I haven’t ever thought to look.  All of the tutorials talk about using the Visual Studio interactive window.

Well, maybe there’s another way.

Ok, but where do I even find the C# interactive exe?

I don’t know… Do you have the C# compiler in your path?

Yes.

Maybe the C# interactive exe is in the same directory.

Good idea… But I added that to my path a long time ago. And now there’s million things in it. So, what directory is my csc exe even in?

In your command line try ‘csc /?’.

Ok…
That didn’t help. There’s just a bunch of commands I can use with csc.

Oh, I know, try ‘where csc’.

Nice. That worked. It says my csc exe is in ‘Program Files (x86)\MSBuild\14.0\Bin\’. Even better, I see a csi exe in that same directory.

That’s probably it, you know,  ‘C Sharp Interactive’… csi, get it. Try typing ‘csi -?’ on the command line.

That worked! I have some options. But, none of them look like anything I want to use right now.

Maybe just try ‘csi’?

I’m in! There’s another help menu in the interactive mode. Typing ‘#help’ shows some options for working with the interactive window itself. Looks like we can even write a C# script (*.csx) file?!  What’s that all about?

Awesome! But that sounds like a future post. Can you run your code now?

Yeah, I sure can:
WriteLine($"DateTime.Now={DateTime.Now}, DateTimeOffset.Now={DateTimeOffset.Now}")

That worked. No semi-colon needed.  Do you suppose the F# interactive mode will work on the command line, too?

Well, hit Control+c to quit the C# interactive mode, then type ‘where fsi’ in the command line.

Won’t I have to have fsi exe in my path before I can use the ‘where’ command?

I already put there. It’s magic.

Uh… OK. There it is ‘Program Files (x86)\Microsoft SDKs\F#\4.0\’.<\em>

You’ll have to give ‘fsi’ a try.

Author: adam_stidham

Hmmm... maybe I should put something deep and riveting here?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s